Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.io.BufferedReader; |
| 18 | import java.io.FileReader; |
| 19 | import java.io.InputStream; |
| 20 | import java.io.OutputStream; |
| 21 | import java.lang.reflect.InvocationTargetException; |
| 22 | import java.lang.reflect.Method; |
David Srbecky | 3da7608 | 2015-06-10 21:52:06 +0000 | [diff] [blame] | 23 | import java.util.Arrays; |
| 24 | import java.util.Comparator; |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 25 | |
David Srbecky | 3da7608 | 2015-06-10 21:52:06 +0000 | [diff] [blame] | 26 | public class Main implements Comparator<Main> { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 27 | // Whether to test local unwinding. Libunwind uses linker info to find executables. As we do |
| 28 | // not dlopen at the moment, this doesn't work, so keep it off for now. |
David Srbecky | 1baabf0 | 2015-06-16 17:12:34 +0000 | [diff] [blame] | 29 | public final static boolean TEST_LOCAL_UNWINDING = true; |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 30 | |
| 31 | // Unwinding another process, modelling debuggerd. This doesn't use the linker, so should work |
| 32 | // no matter whether we're using dlopen or not. |
| 33 | public final static boolean TEST_REMOTE_UNWINDING = true; |
| 34 | |
| 35 | private boolean secondary; |
| 36 | |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 37 | private boolean full_signatures; |
| 38 | |
David Srbecky | 3da7608 | 2015-06-10 21:52:06 +0000 | [diff] [blame] | 39 | private boolean passed; |
| 40 | |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 41 | public Main(boolean secondary, boolean full_signatures) { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 42 | this.secondary = secondary; |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 43 | this.full_signatures = full_signatures; |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | public static void main(String[] args) throws Exception { |
Mathieu Chartier | 031768a | 2015-08-27 10:25:02 -0700 | [diff] [blame] | 47 | System.loadLibrary(args[0]); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 48 | boolean secondary = false; |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 49 | boolean full_signatures = false; |
| 50 | for (String arg : args) { |
| 51 | if (arg.equals("--secondary")) { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 52 | secondary = true; |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 53 | } |
| 54 | if (arg.equals("--full-signatures")) { |
| 55 | full_signatures = true; |
| 56 | } |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 57 | } |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 58 | new Main(secondary, full_signatures).run(); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 61 | private void run() { |
| 62 | if (secondary) { |
| 63 | if (!TEST_REMOTE_UNWINDING) { |
| 64 | throw new RuntimeException("Should not be running secondary!"); |
| 65 | } |
| 66 | runSecondary(); |
| 67 | } else { |
| 68 | runPrimary(); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | private void runSecondary() { |
David Srbecky | 3da7608 | 2015-06-10 21:52:06 +0000 | [diff] [blame] | 73 | foo(); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 74 | throw new RuntimeException("Didn't expect to get back..."); |
| 75 | } |
| 76 | |
| 77 | private void runPrimary() { |
| 78 | // First do the in-process unwinding. |
David Srbecky | 3da7608 | 2015-06-10 21:52:06 +0000 | [diff] [blame] | 79 | if (TEST_LOCAL_UNWINDING && !foo()) { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 80 | System.out.println("Unwinding self failed."); |
| 81 | } |
| 82 | |
| 83 | if (!TEST_REMOTE_UNWINDING) { |
| 84 | // Skip the remote step. |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | // Fork the secondary. |
| 89 | String[] cmdline = getCmdLine(); |
| 90 | String[] secCmdLine = new String[cmdline.length + 1]; |
| 91 | System.arraycopy(cmdline, 0, secCmdLine, 0, cmdline.length); |
| 92 | secCmdLine[secCmdLine.length - 1] = "--secondary"; |
| 93 | Process p = exec(secCmdLine); |
| 94 | |
| 95 | try { |
| 96 | int pid = getPid(p); |
| 97 | if (pid <= 0) { |
| 98 | throw new RuntimeException("Couldn't parse process"); |
| 99 | } |
| 100 | |
| 101 | // Wait a bit, so the forked process has time to run until its sleep phase. |
| 102 | try { |
| 103 | Thread.sleep(5000); |
| 104 | } catch (Exception e) { |
| 105 | throw new RuntimeException(e); |
| 106 | } |
| 107 | |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 108 | if (!unwindOtherProcess(full_signatures, pid)) { |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 109 | System.out.println("Unwinding other process failed."); |
| 110 | } |
| 111 | } finally { |
David Srbecky | a70e5b9 | 2015-06-17 03:52:54 +0100 | [diff] [blame] | 112 | // Kill the forked process if it is not already dead. |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 113 | p.destroy(); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | private static Process exec(String[] args) { |
| 118 | try { |
| 119 | return Runtime.getRuntime().exec(args); |
| 120 | } catch (Exception exc) { |
| 121 | throw new RuntimeException(exc); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | private static int getPid(Process p) { |
| 126 | // Could do reflection for the private pid field, but String parsing is easier. |
| 127 | String s = p.toString(); |
| 128 | if (s.startsWith("Process[pid=")) { |
Narayan Kamath | 51147f2 | 2015-11-17 12:09:54 +0000 | [diff] [blame] | 129 | return Integer.parseInt(s.substring("Process[pid=".length(), s.indexOf(","))); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 130 | } else { |
| 131 | return -1; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Read /proc/self/cmdline to find the invocation command line (so we can fork another runtime). |
| 136 | private static String[] getCmdLine() { |
| 137 | try { |
| 138 | BufferedReader in = new BufferedReader(new FileReader("/proc/self/cmdline")); |
| 139 | String s = in.readLine(); |
| 140 | in.close(); |
| 141 | return s.split("\0"); |
| 142 | } catch (Exception exc) { |
| 143 | throw new RuntimeException(exc); |
| 144 | } |
| 145 | } |
| 146 | |
David Srbecky | 3da7608 | 2015-06-10 21:52:06 +0000 | [diff] [blame] | 147 | public boolean foo() { |
| 148 | // Call bar via Arrays.binarySearch. |
| 149 | // This tests that we can unwind from framework code. |
| 150 | Main[] array = { this, this, this }; |
| 151 | Arrays.binarySearch(array, 0, 3, this /* value */, this /* comparator */); |
| 152 | return passed; |
| 153 | } |
| 154 | |
| 155 | public int compare(Main lhs, Main rhs) { |
| 156 | passed = bar(secondary); |
| 157 | // Returning "equal" ensures that we terminate search |
| 158 | // after first item and thus call bar() only once. |
| 159 | return 0; |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | public boolean bar(boolean b) { |
| 163 | if (b) { |
| 164 | return sleep(2, b, 1.0); |
| 165 | } else { |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 166 | return unwindInProcess(full_signatures, 1, b); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | |
| 170 | // Native functions. Note: to avoid deduping, they must all have different signatures. |
| 171 | |
| 172 | public native boolean sleep(int i, boolean b, double dummy); |
| 173 | |
David Srbecky | 5288611 | 2016-01-22 13:56:47 +0000 | [diff] [blame] | 174 | public native boolean unwindInProcess(boolean full_signatures, int i, boolean b); |
| 175 | public native boolean unwindOtherProcess(boolean full_signatures, int pid); |
Andreas Gampe | 7381010 | 2015-04-22 18:57:06 -0700 | [diff] [blame] | 176 | } |