1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import java.lang.reflect.Method;
18 import java.util.ArrayList;
19 import java.util.Base64;
20 import java.util.LinkedList;
21 import java.lang.reflect.Executable;
22 
23 import art.*;
24 
25 public class Main {
26   /**
27    * NB This test cannot be run on the RI.
28    * TODO We should make this run on the RI.
29    */
30 
main(String[] args)31   public static void main(String[] args) throws Exception {
32     doTest();
33   }
34 
35   public static volatile boolean started = false;
36 
doNothing()37   public static void doNothing() {}
notifyBreakpointReached(Thread thr, Executable e, long l)38   public static void notifyBreakpointReached(Thread thr, Executable e, long l) {}
39 
doTest()40   public static void doTest() throws Exception {
41     final Object lock = new Object();
42     Breakpoint.Manager man = new Breakpoint.Manager();
43     Breakpoint.startBreakpointWatch(
44         Main.class,
45         Main.class.getDeclaredMethod("notifyBreakpointReached", Thread.class, Executable.class, Long.TYPE),
46         null);
47     Thread thr = new Thread(() -> {
48       synchronized (lock) {
49         started = true;
50         // Wait basically forever.
51         try {
52           lock.wait(Integer.MAX_VALUE - 1);
53         } catch (Exception e) {
54           throw new Error("WAIT EXCEPTION", e);
55         }
56       }
57     });
58     // set the breakpoint.
59     man.setBreakpoint(Main.class.getDeclaredMethod("doNothing"), 0l);
60     thr.start();
61     while (!started || thr.getState() != Thread.State.TIMED_WAITING);
62     // Redefine while thread is paused.
63     forceRedefine(Object.class, Thread.currentThread());
64     // Clear breakpoints.
65     man.clearAllBreakpoints();
66     // set the breakpoint again.
67     man.setBreakpoint(Main.class.getDeclaredMethod("doNothing"), 0l);
68     // Wakeup
69     synchronized(lock) {
70       lock.notifyAll();
71     }
72     thr.join();
73   }
74 
forceRedefine(Class c, Thread thr)75   private static native void forceRedefine(Class c, Thread thr);
76 }
77