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 import java.lang.reflect.Method; 18*795d594fSAndroid Build Coastguard Worker import java.util.ArrayList; 19*795d594fSAndroid Build Coastguard Worker import java.util.Base64; 20*795d594fSAndroid Build Coastguard Worker import java.util.LinkedList; 21*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Executable; 22*795d594fSAndroid Build Coastguard Worker 23*795d594fSAndroid Build Coastguard Worker import art.*; 24*795d594fSAndroid Build Coastguard Worker 25*795d594fSAndroid Build Coastguard Worker public class Main { 26*795d594fSAndroid Build Coastguard Worker /** 27*795d594fSAndroid Build Coastguard Worker * NB This test cannot be run on the RI. 28*795d594fSAndroid Build Coastguard Worker * TODO We should make this run on the RI. 29*795d594fSAndroid Build Coastguard Worker */ 30*795d594fSAndroid Build Coastguard Worker main(String[] args)31*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) throws Exception { 32*795d594fSAndroid Build Coastguard Worker doTest(); 33*795d594fSAndroid Build Coastguard Worker } 34*795d594fSAndroid Build Coastguard Worker 35*795d594fSAndroid Build Coastguard Worker public static volatile boolean started = false; 36*795d594fSAndroid Build Coastguard Worker doNothing()37*795d594fSAndroid Build Coastguard Worker public static void doNothing() {} notifyBreakpointReached(Thread thr, Executable e, long l)38*795d594fSAndroid Build Coastguard Worker public static void notifyBreakpointReached(Thread thr, Executable e, long l) {} 39*795d594fSAndroid Build Coastguard Worker doTest()40*795d594fSAndroid Build Coastguard Worker public static void doTest() throws Exception { 41*795d594fSAndroid Build Coastguard Worker final Object lock = new Object(); 42*795d594fSAndroid Build Coastguard Worker Breakpoint.Manager manager = new Breakpoint.Manager(); 43*795d594fSAndroid Build Coastguard Worker Breakpoint.startBreakpointWatch( 44*795d594fSAndroid Build Coastguard Worker Main.class, 45*795d594fSAndroid Build Coastguard Worker Main.class.getDeclaredMethod("notifyBreakpointReached", Thread.class, Executable.class, Long.TYPE), 46*795d594fSAndroid Build Coastguard Worker null); 47*795d594fSAndroid Build Coastguard Worker Thread thr = new Thread(() -> { 48*795d594fSAndroid Build Coastguard Worker synchronized (lock) { 49*795d594fSAndroid Build Coastguard Worker started = true; 50*795d594fSAndroid Build Coastguard Worker // Wait basically forever. 51*795d594fSAndroid Build Coastguard Worker try { 52*795d594fSAndroid Build Coastguard Worker lock.wait(Integer.MAX_VALUE - 1); 53*795d594fSAndroid Build Coastguard Worker } catch (Exception e) { 54*795d594fSAndroid Build Coastguard Worker throw new Error("WAIT EXCEPTION", e); 55*795d594fSAndroid Build Coastguard Worker } 56*795d594fSAndroid Build Coastguard Worker } 57*795d594fSAndroid Build Coastguard Worker }); 58*795d594fSAndroid Build Coastguard Worker // set the breakpoint. 59*795d594fSAndroid Build Coastguard Worker manager.setBreakpoint(Main.class.getDeclaredMethod("doNothing"), 0l); 60*795d594fSAndroid Build Coastguard Worker thr.start(); 61*795d594fSAndroid Build Coastguard Worker while (!started || thr.getState() != Thread.State.TIMED_WAITING); 62*795d594fSAndroid Build Coastguard Worker // Redefine while thread is paused. 63*795d594fSAndroid Build Coastguard Worker forceRedefine(Object.class, Thread.currentThread()); 64*795d594fSAndroid Build Coastguard Worker // Clear breakpoints. 65*795d594fSAndroid Build Coastguard Worker manager.clearAllBreakpoints(); 66*795d594fSAndroid Build Coastguard Worker // set the breakpoint again. 67*795d594fSAndroid Build Coastguard Worker manager.setBreakpoint(Main.class.getDeclaredMethod("doNothing"), 0l); 68*795d594fSAndroid Build Coastguard Worker // Wakeup 69*795d594fSAndroid Build Coastguard Worker synchronized(lock) { 70*795d594fSAndroid Build Coastguard Worker lock.notifyAll(); 71*795d594fSAndroid Build Coastguard Worker } 72*795d594fSAndroid Build Coastguard Worker thr.join(); 73*795d594fSAndroid Build Coastguard Worker } 74*795d594fSAndroid Build Coastguard Worker forceRedefine(Class c, Thread thr)75*795d594fSAndroid Build Coastguard Worker private static native void forceRedefine(Class c, Thread thr); 76*795d594fSAndroid Build Coastguard Worker } 77