blob: 77553380c8f970dab6eb10e48ee6dde005bd8327 [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;
19import java.io.InputStream;
20import java.io.OutputStream;
21import java.lang.reflect.InvocationTargetException;
22import java.lang.reflect.Method;
David Srbecky3da76082015-06-10 21:52:06 +000023import java.util.Arrays;
24import java.util.Comparator;
Andreas Gampe73810102015-04-22 18:57:06 -070025
David Srbecky3da76082015-06-10 21:52:06 +000026public class Main implements Comparator<Main> {
Andreas Gampe73810102015-04-22 18:57:06 -070027 // 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 Srbecky1baabf02015-06-16 17:12:34 +000029 public final static boolean TEST_LOCAL_UNWINDING = true;
Andreas Gampe73810102015-04-22 18:57:06 -070030
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 Srbecky52886112016-01-22 13:56:47 +000037 private boolean full_signatures;
38
David Srbecky3da76082015-06-10 21:52:06 +000039 private boolean passed;
40
David Srbecky52886112016-01-22 13:56:47 +000041 public Main(boolean secondary, boolean full_signatures) {
Andreas Gampe73810102015-04-22 18:57:06 -070042 this.secondary = secondary;
David Srbecky52886112016-01-22 13:56:47 +000043 this.full_signatures = full_signatures;
Andreas Gampe73810102015-04-22 18:57:06 -070044 }
45
46 public static void main(String[] args) throws Exception {
Mathieu Chartier031768a2015-08-27 10:25:02 -070047 System.loadLibrary(args[0]);
Andreas Gampe73810102015-04-22 18:57:06 -070048 boolean secondary = false;
David Srbecky52886112016-01-22 13:56:47 +000049 boolean full_signatures = false;
50 for (String arg : args) {
51 if (arg.equals("--secondary")) {
Andreas Gampe73810102015-04-22 18:57:06 -070052 secondary = true;
David Srbecky52886112016-01-22 13:56:47 +000053 }
54 if (arg.equals("--full-signatures")) {
55 full_signatures = true;
56 }
Andreas Gampe73810102015-04-22 18:57:06 -070057 }
David Srbecky52886112016-01-22 13:56:47 +000058 new Main(secondary, full_signatures).run();
Andreas Gampe73810102015-04-22 18:57:06 -070059 }
60
Andreas Gampe73810102015-04-22 18:57:06 -070061 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 Srbecky3da76082015-06-10 21:52:06 +000073 foo();
Andreas Gampe73810102015-04-22 18:57:06 -070074 throw new RuntimeException("Didn't expect to get back...");
75 }
76
77 private void runPrimary() {
78 // First do the in-process unwinding.
David Srbecky3da76082015-06-10 21:52:06 +000079 if (TEST_LOCAL_UNWINDING && !foo()) {
Andreas Gampe73810102015-04-22 18:57:06 -070080 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 Srbecky52886112016-01-22 13:56:47 +0000108 if (!unwindOtherProcess(full_signatures, pid)) {
Andreas Gampe73810102015-04-22 18:57:06 -0700109 System.out.println("Unwinding other process failed.");
110 }
111 } finally {
David Srbeckya70e5b92015-06-17 03:52:54 +0100112 // Kill the forked process if it is not already dead.
Andreas Gampe73810102015-04-22 18:57:06 -0700113 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 Kamath51147f22015-11-17 12:09:54 +0000129 return Integer.parseInt(s.substring("Process[pid=".length(), s.indexOf(",")));
Andreas Gampe73810102015-04-22 18:57:06 -0700130 } 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 Srbecky3da76082015-06-10 21:52:06 +0000147 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 Gampe73810102015-04-22 18:57:06 -0700160 }
161
162 public boolean bar(boolean b) {
163 if (b) {
164 return sleep(2, b, 1.0);
165 } else {
David Srbecky52886112016-01-22 13:56:47 +0000166 return unwindInProcess(full_signatures, 1, b);
Andreas Gampe73810102015-04-22 18:57:06 -0700167 }
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 Srbecky52886112016-01-22 13:56:47 +0000174 public native boolean unwindInProcess(boolean full_signatures, int i, boolean b);
175 public native boolean unwindOtherProcess(boolean full_signatures, int pid);
Andreas Gampe73810102015-04-22 18:57:06 -0700176}