blob: 5cfe33dc590d0cecfdf442441206b11e1579d435 [file] [log] [blame]
Andreas Gampe73810102015-04-22 18:57:06 -07001/*
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
17import java.io.BufferedReader;
18import java.io.FileReader;
David Srbecky03bf1742016-02-15 16:54:34 +000019import java.io.InputStreamReader;
David Srbecky3da76082015-06-10 21:52:06 +000020import java.util.Arrays;
21import java.util.Comparator;
Andreas Gampe73810102015-04-22 18:57:06 -070022
David Srbecky3da76082015-06-10 21:52:06 +000023public class Main implements Comparator<Main> {
David Srbecky02742402016-03-04 14:42:05 +000024 // Whether to test local unwinding.
25 private boolean testLocal;
Andreas Gampe73810102015-04-22 18:57:06 -070026
David Srbecky02742402016-03-04 14:42:05 +000027 // Unwinding another process, modelling debuggerd.
28 private boolean testRemote;
Andreas Gampe73810102015-04-22 18:57:06 -070029
David Srbecky02742402016-03-04 14:42:05 +000030 // We fork ourself to create the secondary process for remote unwinding.
Andreas Gampe73810102015-04-22 18:57:06 -070031 private boolean secondary;
32
David Srbecky02742402016-03-04 14:42:05 +000033 // Expect the symbols to contain full method signatures including parameters.
34 private boolean fullSignatures;
David Srbecky52886112016-01-22 13:56:47 +000035
David Srbecky3da76082015-06-10 21:52:06 +000036 private boolean passed;
37
David Srbecky02742402016-03-04 14:42:05 +000038 public Main(String[] args) throws Exception {
39 System.loadLibrary(args[0]);
40 for (String arg : args) {
41 if (arg.equals("--test-local")) {
42 testLocal = true;
43 }
44 if (arg.equals("--test-remote")) {
45 testRemote = true;
46 }
47 if (arg.equals("--secondary")) {
48 secondary = true;
49 }
50 if (arg.equals("--full-signatures")) {
51 fullSignatures = true;
52 }
53 }
54 if (!testLocal && !testRemote) {
55 System.out.println("No test selected.");
56 }
Andreas Gampe73810102015-04-22 18:57:06 -070057 }
58
59 public static void main(String[] args) throws Exception {
David Srbecky02742402016-03-04 14:42:05 +000060 new Main(args).run();
Andreas Gampe73810102015-04-22 18:57:06 -070061 }
62
Andreas Gampe73810102015-04-22 18:57:06 -070063 private void run() {
64 if (secondary) {
David Srbecky02742402016-03-04 14:42:05 +000065 if (!testRemote) {
Andreas Gampe73810102015-04-22 18:57:06 -070066 throw new RuntimeException("Should not be running secondary!");
67 }
68 runSecondary();
69 } else {
70 runPrimary();
71 }
72 }
73
74 private void runSecondary() {
David Srbecky3da76082015-06-10 21:52:06 +000075 foo();
Andreas Gampe73810102015-04-22 18:57:06 -070076 throw new RuntimeException("Didn't expect to get back...");
77 }
78
79 private void runPrimary() {
80 // First do the in-process unwinding.
David Srbecky02742402016-03-04 14:42:05 +000081 if (testLocal && !foo()) {
Andreas Gampe73810102015-04-22 18:57:06 -070082 System.out.println("Unwinding self failed.");
83 }
84
David Srbecky02742402016-03-04 14:42:05 +000085 if (!testRemote) {
Andreas Gampe73810102015-04-22 18:57:06 -070086 // Skip the remote step.
87 return;
88 }
89
90 // Fork the secondary.
91 String[] cmdline = getCmdLine();
92 String[] secCmdLine = new String[cmdline.length + 1];
93 System.arraycopy(cmdline, 0, secCmdLine, 0, cmdline.length);
94 secCmdLine[secCmdLine.length - 1] = "--secondary";
95 Process p = exec(secCmdLine);
96
97 try {
98 int pid = getPid(p);
99 if (pid <= 0) {
100 throw new RuntimeException("Couldn't parse process");
101 }
102
David Srbecky03bf1742016-02-15 16:54:34 +0000103 // Wait until the forked process had time to run until its sleep phase.
Andreas Gampe73810102015-04-22 18:57:06 -0700104 try {
David Srbecky03bf1742016-02-15 16:54:34 +0000105 InputStreamReader stdout = new InputStreamReader(p.getInputStream(), "UTF-8");
106 BufferedReader lineReader = new BufferedReader(stdout);
107 while (!lineReader.readLine().contains("Going to sleep")) {
108 }
Andreas Gampe73810102015-04-22 18:57:06 -0700109 } catch (Exception e) {
110 throw new RuntimeException(e);
111 }
112
David Srbecky02742402016-03-04 14:42:05 +0000113 if (!unwindOtherProcess(fullSignatures, pid)) {
Andreas Gampe73810102015-04-22 18:57:06 -0700114 System.out.println("Unwinding other process failed.");
115 }
116 } finally {
David Srbeckya70e5b92015-06-17 03:52:54 +0100117 // Kill the forked process if it is not already dead.
Andreas Gampe73810102015-04-22 18:57:06 -0700118 p.destroy();
119 }
120 }
121
122 private static Process exec(String[] args) {
123 try {
124 return Runtime.getRuntime().exec(args);
125 } catch (Exception exc) {
126 throw new RuntimeException(exc);
127 }
128 }
129
130 private static int getPid(Process p) {
131 // Could do reflection for the private pid field, but String parsing is easier.
132 String s = p.toString();
133 if (s.startsWith("Process[pid=")) {
Narayan Kamath51147f22015-11-17 12:09:54 +0000134 return Integer.parseInt(s.substring("Process[pid=".length(), s.indexOf(",")));
Andreas Gampe73810102015-04-22 18:57:06 -0700135 } else {
136 return -1;
137 }
138 }
139
140 // Read /proc/self/cmdline to find the invocation command line (so we can fork another runtime).
141 private static String[] getCmdLine() {
142 try {
143 BufferedReader in = new BufferedReader(new FileReader("/proc/self/cmdline"));
144 String s = in.readLine();
145 in.close();
146 return s.split("\0");
147 } catch (Exception exc) {
148 throw new RuntimeException(exc);
149 }
150 }
151
David Srbecky3da76082015-06-10 21:52:06 +0000152 public boolean foo() {
153 // Call bar via Arrays.binarySearch.
154 // This tests that we can unwind from framework code.
155 Main[] array = { this, this, this };
156 Arrays.binarySearch(array, 0, 3, this /* value */, this /* comparator */);
157 return passed;
158 }
159
160 public int compare(Main lhs, Main rhs) {
161 passed = bar(secondary);
162 // Returning "equal" ensures that we terminate search
163 // after first item and thus call bar() only once.
164 return 0;
Andreas Gampe73810102015-04-22 18:57:06 -0700165 }
166
167 public boolean bar(boolean b) {
168 if (b) {
169 return sleep(2, b, 1.0);
170 } else {
David Srbecky02742402016-03-04 14:42:05 +0000171 return unwindInProcess(fullSignatures, 1, b);
Andreas Gampe73810102015-04-22 18:57:06 -0700172 }
173 }
174
175 // Native functions. Note: to avoid deduping, they must all have different signatures.
176
177 public native boolean sleep(int i, boolean b, double dummy);
178
David Srbecky02742402016-03-04 14:42:05 +0000179 public native boolean unwindInProcess(boolean fullSignatures, int i, boolean b);
180 public native boolean unwindOtherProcess(boolean fullSignatures, int pid);
Andreas Gampe73810102015-04-22 18:57:06 -0700181}