1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2015 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.io.BufferedReader; 18*795d594fSAndroid Build Coastguard Worker import java.io.FileReader; 19*795d594fSAndroid Build Coastguard Worker import java.io.InputStreamReader; 20*795d594fSAndroid Build Coastguard Worker import java.util.Arrays; 21*795d594fSAndroid Build Coastguard Worker import java.util.Comparator; 22*795d594fSAndroid Build Coastguard Worker 23*795d594fSAndroid Build Coastguard Worker public class Main extends Base implements Comparator<Main> { 24*795d594fSAndroid Build Coastguard Worker // Whether to test local unwinding. 25*795d594fSAndroid Build Coastguard Worker private static boolean testLocal; 26*795d594fSAndroid Build Coastguard Worker 27*795d594fSAndroid Build Coastguard Worker // Unwinding another process, modelling debuggerd. 28*795d594fSAndroid Build Coastguard Worker private static boolean testRemote; 29*795d594fSAndroid Build Coastguard Worker 30*795d594fSAndroid Build Coastguard Worker // We fork ourself to create the secondary process for remote unwinding. 31*795d594fSAndroid Build Coastguard Worker private static boolean secondary; 32*795d594fSAndroid Build Coastguard Worker main(String[] args)33*795d594fSAndroid Build Coastguard Worker public static void main(String[] args) throws Exception { 34*795d594fSAndroid Build Coastguard Worker System.out.println("args: " + String.join(" ", Arrays.copyOfRange(args, 1, args.length))); 35*795d594fSAndroid Build Coastguard Worker 36*795d594fSAndroid Build Coastguard Worker System.loadLibrary(args[0]); 37*795d594fSAndroid Build Coastguard Worker for (int i = 1; i < args.length; i++) { 38*795d594fSAndroid Build Coastguard Worker if (args[i].equals("--test-local")) { 39*795d594fSAndroid Build Coastguard Worker testLocal = true; 40*795d594fSAndroid Build Coastguard Worker } else if (args[i].equals("--test-remote")) { 41*795d594fSAndroid Build Coastguard Worker testRemote = true; 42*795d594fSAndroid Build Coastguard Worker } else if (args[i].equals("--secondary")) { 43*795d594fSAndroid Build Coastguard Worker secondary = true; 44*795d594fSAndroid Build Coastguard Worker } else { 45*795d594fSAndroid Build Coastguard Worker System.out.println("Unknown argument: " + args[i]); 46*795d594fSAndroid Build Coastguard Worker System.exit(1); 47*795d594fSAndroid Build Coastguard Worker } 48*795d594fSAndroid Build Coastguard Worker } 49*795d594fSAndroid Build Coastguard Worker 50*795d594fSAndroid Build Coastguard Worker // Call test() via base class to test unwinding through multidex. 51*795d594fSAndroid Build Coastguard Worker new Main().$noinline$runTest(); 52*795d594fSAndroid Build Coastguard Worker } 53*795d594fSAndroid Build Coastguard Worker test()54*795d594fSAndroid Build Coastguard Worker public void test() { 55*795d594fSAndroid Build Coastguard Worker // Call unwind() via Arrays.binarySearch to test unwinding through framework. 56*795d594fSAndroid Build Coastguard Worker Main[] array = { this, this, this }; 57*795d594fSAndroid Build Coastguard Worker Arrays.binarySearch(array, 0, 3, this /* value */, this /* comparator */); 58*795d594fSAndroid Build Coastguard Worker } 59*795d594fSAndroid Build Coastguard Worker compare(Main lhs, Main rhs)60*795d594fSAndroid Build Coastguard Worker public int compare(Main lhs, Main rhs) { 61*795d594fSAndroid Build Coastguard Worker unwind(); 62*795d594fSAndroid Build Coastguard Worker // Returning "equal" ensures that we terminate search 63*795d594fSAndroid Build Coastguard Worker // after first item and thus call unwind() only once. 64*795d594fSAndroid Build Coastguard Worker return 0; 65*795d594fSAndroid Build Coastguard Worker } 66*795d594fSAndroid Build Coastguard Worker unwind()67*795d594fSAndroid Build Coastguard Worker public void unwind() { 68*795d594fSAndroid Build Coastguard Worker if (secondary) { 69*795d594fSAndroid Build Coastguard Worker sigstop(); // This is helper child process. Stop and wait for unwinding. 70*795d594fSAndroid Build Coastguard Worker return; // Don't run the tests again in the secondary helper process. 71*795d594fSAndroid Build Coastguard Worker } 72*795d594fSAndroid Build Coastguard Worker 73*795d594fSAndroid Build Coastguard Worker if (testLocal) { 74*795d594fSAndroid Build Coastguard Worker System.out.println(unwindInProcess() ? "PASS" : "FAIL"); 75*795d594fSAndroid Build Coastguard Worker } 76*795d594fSAndroid Build Coastguard Worker 77*795d594fSAndroid Build Coastguard Worker if (testRemote) { 78*795d594fSAndroid Build Coastguard Worker // Start a secondary helper process. It will stop itself when it is ready. 79*795d594fSAndroid Build Coastguard Worker int pid = startSecondaryProcess(); 80*795d594fSAndroid Build Coastguard Worker // Wait for the secondary process to stop and then unwind it remotely. 81*795d594fSAndroid Build Coastguard Worker System.out.println(unwindOtherProcess(pid) ? "PASS" : "FAIL"); 82*795d594fSAndroid Build Coastguard Worker } 83*795d594fSAndroid Build Coastguard Worker } 84*795d594fSAndroid Build Coastguard Worker startSecondaryProcess()85*795d594fSAndroid Build Coastguard Worker public static native int startSecondaryProcess(); sigstop()86*795d594fSAndroid Build Coastguard Worker public static native boolean sigstop(); unwindInProcess()87*795d594fSAndroid Build Coastguard Worker public static native boolean unwindInProcess(); unwindOtherProcess(int pid)88*795d594fSAndroid Build Coastguard Worker public static native boolean unwindOtherProcess(int pid); 89*795d594fSAndroid Build Coastguard Worker } 90