StrictMode.java revision 1065685400335ef8c1220f34b4e896e7da603789
1438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/* 2438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Copyright (C) 2010 The Android Open Source Project 3438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 4438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Licensed under the Apache License, Version 2.0 (the "License"); 5438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * you may not use this file except in compliance with the License. 6438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * You may obtain a copy of the License at 7438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 8438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * http://www.apache.org/licenses/LICENSE-2.0 9438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 10438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Unless required by applicable law or agreed to in writing, software 11438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * distributed under the License is distributed on an "AS IS" BASIS, 12438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * See the License for the specific language governing permissions and 14438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * limitations under the License. 15438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 16438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpackage android.os; 17438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 18599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrickimport android.animation.ValueAnimator; 19438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.app.ActivityManagerNative; 20bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.app.ActivityThread; 21438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.app.ApplicationErrorReport; 221065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrickimport android.app.IActivityManager; 23bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.content.Intent; 24438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.util.Log; 25cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrickimport android.util.Printer; 26cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrickimport android.util.Singleton; 276804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrickimport android.view.IWindowManager; 28438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 29438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport com.android.internal.os.RuntimeInit; 30438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 31438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport dalvik.system.BlockGuard; 32fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstromimport dalvik.system.CloseGuard; 33438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.PrintWriter; 355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.StringWriter; 365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.util.ArrayList; 3746d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap; 38bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrickimport java.util.concurrent.atomic.AtomicInteger; 3946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 40438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/** 4132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be 4232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix 4332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them. 4415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 4515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or 4615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI 4715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place. Keeping disk 4815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother, 499fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications. By keeping your application's main thread 509fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent 519fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a> 529fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users. 5315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 5415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is 5515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that 5615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency. It's often the case that 5715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be 5815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background 5915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes. If possible, it's best to assume that such 6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p> 6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your 6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or 6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's 6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method: 6615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre> 6815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() { 6915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * if (DEVELOPER_MODE) { 7032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}() 7132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskReads() 7232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskWrites() 7332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectNetwork() // or .detectAll() for all detectable problems 7432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 7532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 7632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}() 7762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * .detectLeakedSqlLiteObjects() 78fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * .detectLeakedClosableObjects() 7932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 8032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyDeath() 8132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 8215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 8315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * super.onCreate(); 8415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 8515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre> 8615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 8732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected. 8832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can 8932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your 9032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen. 9115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 9215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are 9315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler}, 9415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc. 9515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds. In particular, 9632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle. Use 9732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident. Network requests on the UI thread 9815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though. 9915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 10015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not 10115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses. While it does 10215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing 10315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best 10415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism. Notably, disk or network access from JNI calls 10515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it. Future versions of Android may catch 10615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode 10715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * enabled in shipping applications on the Android Market. 108438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 109438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode { 110438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static final String TAG = "StrictMode"; 11182829ef3b7c72bee36d8c17b36ac565f1856a310Brad Fitzpatrick private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE); 112438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1131181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); 1146804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick private static final boolean IS_ENG_BUILD = "eng".equals(Build.TYPE); 1151181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 11646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only log a duplicate stack trace to the logs every second. 11746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_LOG_INTERVAL_MS = 1000; 11846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 11946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only show an annoying dialog at most every 30 seconds 12046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_DIALOG_INTERVAL_MS = 30000; 12146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 122e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // How many Span tags (e.g. animations) to report. 123e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static final int MAX_SPAN_TAGS = 20; 124e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 125191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // How many offending stacks to keep track of (and time) per loop 126191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // of the Looper. 127191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final int MAX_OFFENSES_PER_LOOP = 10; 128191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 12932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Thread-policy: 130438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 13115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 13232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 13315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 13432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_WRITE = 0x01; // for ThreadPolicy 13515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 13615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 13732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 13815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 13932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_READ = 0x02; // for ThreadPolicy 14015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 14115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 14232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 14315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 14432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_NETWORK = 0x04; // for ThreadPolicy 145438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 14632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Process-policy: 147438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 148438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 14932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Note, a "VM_" bit, not thread. 15032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 15132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 15232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_VM_CURSOR_LEAKS = 0x200; // for ProcessPolicy 15332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 15432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 155fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Note, a "VM_" bit, not thread. 156fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 157fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 158fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400; // for ProcessPolicy 159fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 160fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 16132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 162438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 163438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_LOG = 0x10; // normal android.util.Log 164438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 16532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Used for both process and thread policy: 16632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 167438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 16832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 169438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 170438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DIALOG = 0x20; 171438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 172438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 173b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death on any detected violation. 174b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 17532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 176438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 177438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DEATH = 0x40; 178438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 179438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 180b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death just for detected network usage. 181b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 182b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 183b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 184b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static final int PENALTY_DEATH_ON_NETWORK = 0x200; 185b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 186b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 1876804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * Flash the screen during violations. 1886804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * 1896804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * @hide 1906804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick */ 1916804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick public static final int PENALTY_FLASH = 0x800; 1926804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 1936804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick /** 19432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 195438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 196438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DROPBOX = 0x80; 197438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 198727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 199727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Non-public penalty mode which overrides all the other penalty 200727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * bits and signals that we're in a Binder call and we should 201727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * ignore the other penalty bits and instead serialize back all 202727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * our offending stack traces to the caller to ultimately handle 203727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * in the originating process. 204727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * 205703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * This must be kept in sync with the constant in libs/binder/Parcel.cpp 206703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * 207727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * @hide 208727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 209727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick public static final int PENALTY_GATHER = 0x100; 210727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 21132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 21271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick * Mask of all the penalty bits. 21371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick */ 21471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick private static final int PENALTY_MASK = 215b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER | 2166804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH; 21771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 21871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick /** 21932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The current VmPolicy in effect. 22032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 22132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static volatile int sVmPolicyMask = 0; 22232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 223bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick /** 224bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * The number of threads trying to do an async dropbox write. 225bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * Just to limit ourselves out of paranoia. 226bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick */ 227bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0); 228bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 22932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private StrictMode() {} 23032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 23132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 23232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to a certain thread. 23332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 23432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setThreadPolicy}. The current policy 23532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can be retrieved with {@link #getThreadPolicy}. 23632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 23732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note that multiple penalties may be provided and they're run 23832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * in order from least to most severe (logging before process 23932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * death, for example). There's currently no mechanism to choose 24032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * different penalties for different detected actions. 24132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 24232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class ThreadPolicy { 24332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 24432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 24532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 24632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final ThreadPolicy LAX = new ThreadPolicy(0); 24732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 24832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 24932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 25032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private ThreadPolicy(int mask) { 25132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 25232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 25332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 25432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 25532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 25632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.ThreadPolicy; mask=" + mask + "]"; 25732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 25832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 25932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 26032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Creates ThreadPolicy instances. Methods whose names start 26132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 26232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 26332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 26432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 26532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 26632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 26732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 26832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 26932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 27032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 27132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() 27232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 27332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 27432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 27532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(policy); 27632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 27732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 27832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 27932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask = 0; 28032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 28132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 28232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Create a Builder that detects nothing and has no 28332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations. (but note that {@link #build} will default 28432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to enabling {@link #penaltyLog} if no other penalties 28532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * are specified) 28632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 28732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder() { 28832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = 0; 28932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 29032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 29132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 29232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Initialize a Builder from an existing ThreadPolicy. 29332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 29432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder(ThreadPolicy policy) { 29532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = policy.mask; 29632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 29732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 29832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 29932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 30032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>As of the Gingerbread release this includes network and 30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * disk operations but will likely expand in future releases. 30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK); 30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable the detection of everything. 31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitAll() { 31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK); 31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of network operations. 31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectNetwork() { 31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_NETWORK); 32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of network operations. 32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitNetwork() { 32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_NETWORK); 32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk reads. 33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 33232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskReads() { 33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_READ); 33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk reads. 33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskReads() { 34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_READ); 34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk writes. 34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 34632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskWrites() { 34732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_WRITE); 34832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk writes. 35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskWrites() { 35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_WRITE); 35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Show an annoying dialog to the developer on detected 35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations, rate-limited to be only a little annoying. 36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 36132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDialog() { 36232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DIALOG); 36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crash the whole process on violation. This penalty runs at 36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so you'll still get 36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * see logging or other violations before the process dies. 369b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 370b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies 371b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * to disk reads, disk writes, and network usage if their 372b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * corresponding detect flags are set. 37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 37632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 37732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 37832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 379b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Crash the whole process on any network usage. Unlike 380b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * {@link #penaltyDeath}, this penalty runs 381b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <em>before</em> anything else. You must still have 382b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * called {@link #detectNetwork} to enable this. 383b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 384b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>In the Honeycomb or later SDKs, this is on by default. 385b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 386b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public Builder penaltyDeathOnNetwork() { 387b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick return enable(PENALTY_DEATH_ON_NETWORK); 388b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 389b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 390b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 3916804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * Flash the screen during a violation. 3926804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick */ 3936804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick public Builder penaltyFlashScreen() { 3946804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick return enable(PENALTY_FLASH); 3956804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 3966804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 3976804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick /** 39832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 39932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 40032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 40132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 40232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 40332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 40432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 40532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 40632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 40732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 41032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 41132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 41232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 41332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 41432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 41532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 41632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 41732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 41832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 41932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder disable(int bit) { 42032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask &= ~bit; 42132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 42232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 42332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 42432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 42532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the ThreadPolicy instance. 42632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 42732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 42832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 42932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public ThreadPolicy build() { 43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 43332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 43432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 43532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(mMask); 44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 44432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 44532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to all threads in the virtual machine's process. 44632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 44732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setVmPolicy}. 44832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 44932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class VmPolicy { 45032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 45132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 45232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 45332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final VmPolicy LAX = new VmPolicy(0); 45432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 45532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 45632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 45732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private VmPolicy(int mask) { 45832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 45932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 46032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 46132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 46232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 46332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.VmPolicy; mask=" + mask + "]"; 46432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 46532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 46632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 46732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Creates {@link VmPolicy} instances. Methods whose names start 46832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 46932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 47032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 47132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 47232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 47332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 47432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 47532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 47632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 47732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 47832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() 47932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 48032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 48132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 48232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(policy); 48332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 48432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask; 48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 49032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 491fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>In the Honeycomb release this includes leaks of 492fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * SQLite cursors and other closable objects but will 493fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * likely expand in future releases. 49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 496fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return enable(DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS); 49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 49832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 49932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 50032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect when an 50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link android.database.sqlite.SQLiteCursor} or other 50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * SQLite object is finalized without having been closed. 50332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You always want to explicitly close your SQLite 50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * cursors to avoid unnecessary database contention and 50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * temporary memory leaks. 50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectLeakedSqlLiteObjects() { 50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_VM_CURSOR_LEAKS); 51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 513fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Detect when an {@link java.io.Closeable} or other 514fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * object with a explict termination method is finalized 515fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * without having been closed. 516fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * 517fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>You always want to explicitly close such objects to 518fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * avoid unnecessary resources leaks. 519fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 520fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public Builder detectLeakedClosableObjects() { 521fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return enable(DETECT_VM_CLOSABLE_LEAKS); 522fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 523fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 524fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 52532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crashes the whole process on violation. This penalty runs at 52632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so yo you'll still get 52732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * your logging or other violations before the process dies. 52832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 52932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 53032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 53132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 53332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 53832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 53932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 54032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 54232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 54332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 54432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 54532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 54632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 54732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 54832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 54932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 55032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 55132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 55232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the VmPolicy instance. 55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 55832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 55932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 56032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 56132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 56232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public VmPolicy build() { 56332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 56432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 56532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 56632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 56732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 56832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 56932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 57032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new VmPolicy(mMask); 57132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 57232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 57332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 574438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 575438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 5765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Log of strict mode violation stack traces that have occurred 5775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * during a Binder call, to be serialized back later to the caller 5785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * via Parcel.writeNoException() (amusingly) where the caller can 5795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * choose how to react. 5805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 581cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations = 582cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 583cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 584703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // Starts null to avoid unnecessary allocations when 585703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // checking whether there are any violations or not in 586703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // hasGatheredViolations() below. 587703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return null; 5885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 5895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick }; 5905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 5915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 59232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions on the current thread should 59332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * be detected, as well as the penalty if such actions occur. 59415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 59532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Internally this sets a thread-local variable which is 59615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagated across cross-process IPC calls, meaning you can 59715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * catch violations when a system service or another process 59815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * accesses the disk or network on your behalf. 599438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 60032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 601438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 60232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setThreadPolicy(final ThreadPolicy policy) { 60332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(policy.mask); 60432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 60532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 60632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static void setThreadPolicyMask(final int policyMask) { 607727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // In addition to the Java-level thread-local in Dalvik's 608727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // BlockGuard, we also need to keep a native thread-local in 609727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Binder in order to propagate the value across Binder calls, 610727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // even across native-only processes. The two are kept in 611727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // sync via the callback to onStrictModePolicyChange, below. 612727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(policyMask); 613727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 614727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // And set the Android native version... 615727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick Binder.setThreadStrictModePolicy(policyMask); 616727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 617727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 618727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Sets the policy in Dalvik/libcore (BlockGuard) 619727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void setBlockGuardPolicy(final int policyMask) { 62046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick if (policyMask == 0) { 62146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY); 62246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick return; 62346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 624438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 625438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick if (!(policy instanceof AndroidBlockGuardPolicy)) { 626438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick BlockGuard.setThreadPolicy(new AndroidBlockGuardPolicy(policyMask)); 627438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 628438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick AndroidBlockGuardPolicy androidPolicy = (AndroidBlockGuardPolicy) policy; 629438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick androidPolicy.setPolicyMask(policyMask); 630438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 631438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 632438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 6334b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom // Sets up CloseGuard in Dalvik/libcore 6344b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom private static void setCloseGuardEnabled(boolean enabled) { 6357c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) { 6364b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom CloseGuard.setReporter(new AndroidCloseGuardReporter()); 6374b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 6384b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom CloseGuard.setEnabled(enabled); 6394b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 6404b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 6415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeNetworkViolation extends BlockGuard.BlockGuardPolicyException { 6425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeNetworkViolation(int policyMask) { 64332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_NETWORK); 6445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 6475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeDiskReadViolation extends BlockGuard.BlockGuardPolicyException { 6485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskReadViolation(int policyMask) { 64932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_DISK_READ); 6505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 6535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeDiskWriteViolation extends BlockGuard.BlockGuardPolicyException { 6545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskWriteViolation(int policyMask) { 65532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_DISK_WRITE); 6565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 659438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 66015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * Returns the bitmask of the current thread's policy. 661438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 66232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled 66332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 66432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 665438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 66632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static int getThreadPolicyMask() { 667438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return BlockGuard.getThreadPolicy().getPolicyMask(); 668438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 669438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 6705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 67132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Returns the current thread's policy. 67232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 67332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy getThreadPolicy() { 67432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(getThreadPolicyMask()); 67532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 67632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 67732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 67832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 67932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 68032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit both disk reads & writes, and sets the new policy 68132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 68232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 68397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 68432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to {@link #setThreadPolicy} to 68532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * restore the policy at the end of a block 68697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskWrites() { 68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 68932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ); 69032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 69297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 69332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 69497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 69597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 69697461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 69732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 69832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 69932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit disk reads, and sets the new policy 70032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 70132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 70297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 70332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to setThreadPolicy to 70497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * restore the policy. 70597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 70632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskReads() { 70732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 70832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ); 70932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 71032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 71197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 71232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 71397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 71497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 71597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 71650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * Enable DropBox logging for debug phone builds. 71750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * 71850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * @hide 71950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick */ 72050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick public static boolean conditionallyEnableDebugLogging() { 72150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // For debug builds, log event loop stalls to dropbox for analysis. 72250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // Similar logic also appears in ActivityThread.java for system apps. 7231181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick if (IS_USER_BUILD) { 7247c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick setCloseGuardEnabled(false); 72550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return false; 72650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 72732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.setThreadPolicyMask( 72832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.DETECT_DISK_WRITE | 72932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.DETECT_DISK_READ | 73032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.DETECT_NETWORK | 7316804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick StrictMode.PENALTY_DROPBOX | 7326804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick (IS_ENG_BUILD ? StrictMode.PENALTY_FLASH : 0) 7336804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick ); 73432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick sVmPolicyMask = StrictMode.DETECT_VM_CURSOR_LEAKS | 735fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom StrictMode.DETECT_VM_CLOSABLE_LEAKS | 736d188ededa6f61dbe5a700e81db22c02478ce60ccBrad Fitzpatrick StrictMode.PENALTY_DROPBOX; 7377c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); 73850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return true; 73950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 74050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick 74150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick /** 742b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Used by the framework to make network usage on the main 743b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * thread a fatal error. 744b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 745b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 746b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 747b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static void enableDeathOnNetwork() { 748b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int oldPolicy = getThreadPolicyMask(); 749b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK; 750b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick setThreadPolicyMask(newPolicy); 751b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 752b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 753b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 7545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Parses the BlockGuard policy mask out from the Exception's 7555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * getMessage() String value. Kinda gross, but least 7565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * invasive. :/ 7575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 7585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Input is of form "policy=137 violation=64" 7595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 7605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Returns 0 on failure, which is a valid policy, but not a 7615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * valid policy during a violation (else there must've been 7625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * some policy in effect to violate). 7635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 7645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parsePolicyFromMessage(String message) { 7655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null || !message.startsWith("policy=")) { 7665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7685b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int spaceIndex = message.indexOf(' '); 7695b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (spaceIndex == -1) { 7705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7715b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7725b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String policyString = message.substring(7, spaceIndex); 7735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 7745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(policyString).intValue(); 7755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 7765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 7805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 7815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Like parsePolicyFromMessage(), but returns the violation. 7825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 7835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parseViolationFromMessage(String message) { 7845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null) { 7855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int violationIndex = message.indexOf("violation="); 7885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (violationIndex == -1) { 7895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String violationString = message.substring(violationIndex + 10); 7925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 7935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(violationString).intValue(); 7945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 7955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 799191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed = 800191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 801191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 802191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return new ArrayList<ViolationInfo>(); 803191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 804191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick }; 805191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 806191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static boolean tooManyViolationsThisLoop() { 807191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP; 808191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 809191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 810438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static class AndroidBlockGuardPolicy implements BlockGuard.Policy { 811438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private int mPolicyMask; 81246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 81346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Map from violation stacktrace hashcode -> uptimeMillis of 81446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // last violation. No locking needed, as this is only 81546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // accessed by the same thread. 81646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private final HashMap<Integer, Long> mLastViolationTime = new HashMap<Integer, Long>(); 817438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 818438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public AndroidBlockGuardPolicy(final int policyMask) { 819438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 820438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 821438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 8225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick @Override 8235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public String toString() { 8245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask; 8255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 827438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 828438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public int getPolicyMask() { 829438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return mPolicyMask; 830438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 831438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 832438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 833438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onWriteToDisk() { 83432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_WRITE) == 0) { 835438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 836438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 837191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 838191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 839191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 840cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask); 841cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 842cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 843438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 844438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 845438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 846438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onReadFromDisk() { 84732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_READ) == 0) { 848438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 849438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 850191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 851191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 852191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 853cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask); 854cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 855cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 856438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 857438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 858438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 859438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onNetwork() { 86032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_NETWORK) == 0) { 861438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 862438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 863b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) { 864b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick throw new NetworkOnMainThreadException(); 865b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 866191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 867191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 868191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 869cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask); 870cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 871cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 872438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 873438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 874438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void setPolicyMask(int policyMask) { 875438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 876438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 877438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 8785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Start handling a violation that just started and hasn't 8795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // actually run yet (e.g. no disk write or network operation 8805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // has yet occurred). This sees if we're in an event loop 8815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread and, if so, uses it to roughly measure how long the 8825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation took. 8835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) { 884cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ViolationInfo info = new ViolationInfo(e, e.getPolicy()); 885cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.violationUptimeMillis = SystemClock.uptimeMillis(); 886cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolationWithTimingAttempt(info); 887cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 888438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 889cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Attempts to fill in the provided ViolationInfo's 890cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // durationMillis field if this thread has a Looper we can use 891cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // to measure with. We measure from the time of violation 892cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // until the time the looper is idle again (right before 893cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // the next epoll_wait) 894cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolationWithTimingAttempt(final ViolationInfo info) { 895438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick Looper looper = Looper.myLooper(); 896cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 897cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Without a Looper, we're unable to time how long the 898cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // violation takes place. This case should be rare, as 899cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // most users will care about timing violations that 900cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // happen on their main UI thread. Note that this case is 901cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // also hit when a violation takes place in a Binder 902cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // thread, in "gather" mode. In this case, the duration 903cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // of the violation is computed by the ultimate caller and 904cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // its Looper, if any. 905cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // TODO: if in gather mode, ignore Looper.myLooper() and always 906cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // go into this immediate mode? 907438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick if (looper == null) { 908cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis = -1; // unknown (redundant, already set) 909cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(info); 910cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 911438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 912438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 913cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick MessageQueue queue = Looper.myQueue(); 914cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ArrayList<ViolationInfo> records = violationsBeingTimed.get(); 915191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (records.size() >= MAX_OFFENSES_PER_LOOP) { 916cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Not worth measuring. Too many offenses in one loop. 917cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 918cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 919cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.add(info); 920cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (records.size() > 1) { 921cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // There's already been a violation this loop, so we've already 922cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // registered an idle handler to process the list of violations 923cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // at the end of this Looper's loop. 924cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 925cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 926cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 9276804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ? 928cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick sWindowManager.get() : null; 9296804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick if (windowManager != null) { 9306804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick try { 9316804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick windowManager.showStrictModeViolation(true); 9326804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } catch (RemoteException unused) { 9336804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 9346804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 9356804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 936cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick queue.addIdleHandler(new MessageQueue.IdleHandler() { 937cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public boolean queueIdle() { 938cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick long loopFinishTime = SystemClock.uptimeMillis(); 939cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (int n = 0; n < records.size(); ++n) { 940cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo v = records.get(n); 941cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.violationNumThisLoop = n + 1; 942cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.durationMillis = 943cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick (int) (loopFinishTime - v.violationUptimeMillis); 944cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(v); 945cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 946cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.clear(); 9476804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick if (windowManager != null) { 9486804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick try { 9496804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick windowManager.showStrictModeViolation(false); 9506804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } catch (RemoteException unused) { 9516804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 9526804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 953cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return false; // remove this idle handler from the array 954cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 955cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick }); 9565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 957438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 9585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Note: It's possible (even quite likely) that the 9595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread-local policy mask has changed from the time the 9605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation fired and now (after the violating code ran) due 9615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // to people who push/pop temporary policy in regions of code, 9625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // hence the policy being passed around. 963cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolation(final ViolationInfo info) { 964cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) { 965cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.wtf(TAG, "unexpected null stacktrace"); 9665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 9675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 968438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 969cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy); 97046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 971cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_GATHER) != 0) { 972cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 973703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 974cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations = new ArrayList<ViolationInfo>(1); 975703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(violations); 976703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else if (violations.size() >= 5) { 9775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Too many. In a loop or something? Don't gather them all. 9785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 9795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 980cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (ViolationInfo previous : violations) { 981cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) { 9825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Duplicate. Don't log. 9835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 9845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 9855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 986cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations.add(info); 987727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick return; 988727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 989727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 99046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Not perfect, but fast and good enough for dup suppression. 991f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick Integer crashFingerprint = info.hashCode(); 99246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long lastViolationTime = 0; 99346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick if (mLastViolationTime.containsKey(crashFingerprint)) { 99446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick lastViolationTime = mLastViolationTime.get(crashFingerprint); 99546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 99646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long now = SystemClock.uptimeMillis(); 99746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick mLastViolationTime.put(crashFingerprint, now); 99846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long timeSinceLastViolationMillis = lastViolationTime == 0 ? 99946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Long.MAX_VALUE : (now - lastViolationTime); 100046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1001cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_LOG) != 0 && 100246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { 1003cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.durationMillis != -1) { 10045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation; ~duration=" + 1005cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis + " ms: " + info.crashInfo.stackTrace); 1006438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 1007cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace); 1008438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1009438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1010438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 101171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // The violationMaskSubset, passed to ActivityManager, is a 101246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // subset of the original StrictMode policy bitmask, with 101346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // only the bit violated and penalty bits to be executed 101446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // by the ActivityManagerService remaining set. 1015cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationMaskSubset = 0; 101646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1017cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DIALOG) != 0 && 101846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) { 1019cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DIALOG; 102046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 102146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1022cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) { 1023cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DROPBOX; 102446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 102546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1026cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationMaskSubset != 0) { 1027cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage); 1028cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= violationBit; 102932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 103071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 103171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick final boolean justDropBox = (info.policy & PENALTY_MASK) == PENALTY_DROPBOX; 103271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick if (justDropBox) { 103371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // If all we're going to ask the activity manager 103471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // to do is dropbox it (the common case during 103571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // platform development), we can avoid doing this 103671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // call synchronously which Binder data suggests 103771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // isn't always super fast, despite the implementation 103871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // in the ActivityManager trying to be mostly async. 1039bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick dropboxViolationAsync(violationMaskSubset, info); 104071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick return; 104171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick } 104271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 104371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // Normal synchronous call to the ActivityManager. 1044438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick try { 1045727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 1046727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 1047727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 1048727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // We restore the current policy below, in the finally block. 104932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 1050727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 1051438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 1052438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick RuntimeInit.getApplicationObject(), 1053cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset, 1054cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info); 1055438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } catch (RemoteException e) { 105646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 1057727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } finally { 1058727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Restore the policy. 105932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 1060438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1061438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1062438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1063cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DEATH) != 0) { 1064438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick System.err.println("StrictMode policy violation with POLICY_DEATH; shutting down."); 1065438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick Process.killProcess(Process.myPid()); 1066438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick System.exit(10); 1067438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1068438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1069438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1070727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 1071bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick /** 1072bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * In the common case, as set by conditionallyEnableDebugLogging, 1073bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * we're just dropboxing any violations but not showing a dialog, 1074bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * not loggging, and not killing the process. In these cases we 1075bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * don't need to do a synchronous call to the ActivityManager. 1076bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * This is used by both per-thread and vm-wide violations when 1077bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * applicable. 1078bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick */ 1079bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick private static void dropboxViolationAsync( 1080bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick final int violationMaskSubset, final ViolationInfo info) { 1081bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int outstanding = sDropboxCallsInFlight.incrementAndGet(); 1082bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (outstanding > 20) { 1083bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // What's going on? Let's not make make the situation 1084bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // worse and just not log. 1085bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick sDropboxCallsInFlight.decrementAndGet(); 1086bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick return; 1087bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1088bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1089bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding); 1090bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1091bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick new Thread("callActivityManagerForStrictModeDropbox") { 1092bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick public void run() { 1093bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 1094bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick try { 10951065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick IActivityManager am = ActivityManagerNative.getDefault(); 10961065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick if (am == null) { 10971065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick Log.d(TAG, "No activity manager; failed to Dropbox violation."); 10981065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick } else { 10991065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick am.handleApplicationStrictModeViolation( 11001065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick RuntimeInit.getApplicationObject(), 11011065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick violationMaskSubset, 11021065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick info); 11031065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick } 1104bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } catch (RemoteException e) { 1105bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick Log.e(TAG, "RemoteException handling StrictMode violation", e); 1106bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1107bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int outstanding = sDropboxCallsInFlight.decrementAndGet(); 1108bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding); 1109bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1110bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick }.start(); 1111bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1112bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 11134b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom private static class AndroidCloseGuardReporter implements CloseGuard.Reporter { 11144b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom public void report (String message, Throwable allocationSite) { 11154b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom onVmPolicyViolation(message, allocationSite); 11164b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 11174b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 11184b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 1119727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 11205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 11215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 11225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static boolean hasGatheredViolations() { 1123703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return gatheredViolations.get() != null; 1124703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1125703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick 1126703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /** 1127703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Called from Parcel.writeException(), so we drop this memory and 1128703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * don't incorrectly attribute it to the wrong caller on the next 1129703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Binder call on this thread. 1130703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick */ 1131703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /* package */ static void clearGatheredViolations() { 1132703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 11335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 11345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 11355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 113632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions in the VM process (on any 113732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * thread) should be detected, as well as the penalty if such 113832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * actions occur. 113932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 114032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 114132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 114232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setVmPolicy(final VmPolicy policy) { 114332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick sVmPolicyMask = policy.mask; 11444b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); 114532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 114632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 114732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 114832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Gets the current VM policy. 114932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 115032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static VmPolicy getVmPolicy() { 115132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new VmPolicy(sVmPolicyMask); 115232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 115332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 115432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 115562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * Enable the recommended StrictMode defaults, with violations just being logged. 115662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * 115762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * <p>This catches disk and network access on the main thread, as 1158fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * well as leaked SQLite cursors and unclosed resources. This is 1159fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * simply a wrapper around {@link #setVmPolicy} and {@link 1160fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * #setThreadPolicy}. 116162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick */ 116262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick public static void enableDefaults() { 116362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 116462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .detectAll() 116562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 116662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 116762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 116862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .detectLeakedSqlLiteObjects() 1169fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom .detectLeakedClosableObjects() 117062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 117162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 117262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick } 117362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick 117462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick /** 117532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 117632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 117732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static boolean vmSqliteObjectLeaksEnabled() { 117832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0; 117932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 118032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 118132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 118232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 118332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 1184fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public static boolean vmClosableObjectLeaksEnabled() { 1185fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0; 1186fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 1187fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 1188fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 1189fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 1190fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 119132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void onSqliteObjectLeaked(String message, Throwable originStack) { 11924b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom onVmPolicyViolation(message, originStack); 11934b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 11944b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 11954b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom /** 11964b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom * @hide 11974b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom */ 11984b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom public static void onVmPolicyViolation(String message, Throwable originStack) { 119932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((sVmPolicyMask & PENALTY_LOG) != 0) { 120032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Log.e(TAG, message, originStack); 120132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 120232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1203bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0; 1204bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0; 1205bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1206bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int violationMaskSubset = PENALTY_DROPBOX | DETECT_VM_CURSOR_LEAKS; 1207bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask); 1208bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1209bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDropbox && !penaltyDeath) { 1210bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // Common case for userdebug/eng builds. If no death and 1211bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // just dropboxing, we can do the ActivityManager call 1212bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // asynchronously. 1213bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick dropboxViolationAsync(violationMaskSubset, info); 1214bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick return; 1215bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 121632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1217bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDropbox) { 121832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // The violationMask, passed to ActivityManager, is a 121932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // subset of the original StrictMode policy bitmask, with 122032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // only the bit violated and penalty bits to be executed 122132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // by the ActivityManagerService remaining set. 122232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 122332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick try { 122432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 122532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 122632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 122732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // We restore the current policy below, in the finally block. 122832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 122932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 123032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 123132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick RuntimeInit.getApplicationObject(), 123232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick violationMaskSubset, 123332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick info); 123432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } catch (RemoteException e) { 123532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 123632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } finally { 123732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Restore the policy. 123832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 123932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 124032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 124132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1242bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDeath) { 124332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down."); 124432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Process.killProcess(Process.myPid()); 124532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.exit(10); 124632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 124732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 124832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 124932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 12505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 12515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 12525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void writeGatheredViolationsToParcel(Parcel p) { 1253cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 1254703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 1255703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(0); 1256703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else { 1257703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(violations.size()); 1258703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick for (int i = 0; i < violations.size(); ++i) { 1259703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.get(i).writeToParcel(p, 0 /* unused flags? */); 1260703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1261703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size()); 1262703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.clear(); // somewhat redundant, as we're about to null the threadlocal 12635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1264703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 12655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 12665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 12675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class LogStackTrace extends Exception {} 12685b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 12695b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 12705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS, 12715b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * we here read back all the encoded violations. 12725b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 12735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void readAndHandleBinderCallViolations(Parcel p) { 12745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Our own stack trace to append 12755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick StringWriter sw = new StringWriter(); 1276cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new LogStackTrace().printStackTrace(new PrintWriter(sw)); 12775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String ourStack = sw.toString(); 12785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 127932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int policyMask = getThreadPolicyMask(); 1280cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0; 12815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 12825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int numViolations = p.readInt(); 12835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick for (int i = 0; i < numViolations; ++i) { 12845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call. i=" + i); 1285cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo info = new ViolationInfo(p, !currentlyGathering); 1286cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack; 12875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 12885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (policy instanceof AndroidBlockGuardPolicy) { 1289cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info); 12905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 12915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 12925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 12935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 12945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 1295727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Called from android_util_Binder.cpp's 1296727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * android_os_Parcel_enforceInterface when an incoming Binder call 1297727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * requires changing the StrictMode policy mask. The role of this 1298727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * function is to ask Binder for its current (native) thread-local 1299727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * policy value and synchronize it to libcore's (Java) 1300727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * thread-local policy value. 1301727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 1302727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void onBinderStrictModePolicyChange(int newPolicy) { 1303727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(newPolicy); 1304727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 1305cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1306cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1307e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * A tracked, critical time span. (e.g. during an animation.) 1308e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1309e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * The object itself is a linked list node, to avoid any allocations 1310e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * during rapid span entries and exits. 1311e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1312e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1313e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1314e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public static class Span { 1315e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private String mName; 1316e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private long mCreateMillis; 1317e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private Span mNext; 1318e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private Span mPrev; // not used when in freeList, only active 1319e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private final ThreadSpanState mContainerState; 1320e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1321e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span(ThreadSpanState threadState) { 1322e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mContainerState = threadState; 1323e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1324e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 13251181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // Empty constructor for the NO_OP_SPAN 13261181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick protected Span() { 13271181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick mContainerState = null; 13281181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 13291181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 1330e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1331e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * To be called when the critical span is complete (i.e. the 1332e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * animation is done animating). This can be called on any 1333e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * thread (even a different one from where the animation was 1334e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * taking place), but that's only a defensive implementation 1335e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * measure. It really makes no sense for you to call this on 1336e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * thread other than that where you created it. 1337e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1338e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1339e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1340e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public void finish() { 1341e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = mContainerState; 1342e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1343e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mName == null) { 1344e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Duplicate finish call. Ignore. 1345e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return; 1346e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1347e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1348e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Remove ourselves from the active list. 1349e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mPrev != null) { 1350e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mPrev.mNext = mNext; 1351e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1352e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mNext != null) { 1353e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mNext.mPrev = mPrev; 1354e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1355e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mActiveHead == this) { 1356e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveHead = mNext; 1357e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1358e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 13591cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick state.mActiveSize--; 13601cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick 13611cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize); 13621cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick 1363e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mCreateMillis = -1; 1364e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mName = null; 1365e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mPrev = null; 1366e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mNext = null; 1367e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1368e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Add ourselves to the freeList, if it's not already 1369e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // too big. 1370e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mFreeListSize < 5) { 1371e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mNext = state.mFreeListHead; 1372e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListHead = this; 1373e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListSize++; 1374e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1375e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1376e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1377e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1378e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 13791181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // The no-op span that's used in user builds. 13801181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick private static final Span NO_OP_SPAN = new Span() { 13811181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick public void finish() { 13821181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // Do nothing. 13831181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 13841181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick }; 13851181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 1386e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1387e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Linked lists of active spans and a freelist. 1388e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1389e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Locking notes: there's one of these structures per thread and 1390e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * all members of this structure (as well as the Span nodes under 1391e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * it) are guarded by the ThreadSpanState object instance. While 1392e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * in theory there'd be no locking required because it's all local 1393e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * per-thread, the finish() method above is defensive against 1394e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * people calling it on a different thread from where they created 1395e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * the Span, hence the locking. 1396e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1397e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static class ThreadSpanState { 1398e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public Span mActiveHead; // doubly-linked list. 1399e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public int mActiveSize; 1400e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public Span mFreeListHead; // singly-linked list. only changes at head. 1401e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public int mFreeListSize; 1402e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1403e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1404e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState = 1405e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick new ThreadLocal<ThreadSpanState>() { 1406e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick @Override protected ThreadSpanState initialValue() { 1407e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return new ThreadSpanState(); 1408e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1409e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick }; 1410e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1411cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() { 1412cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick protected IWindowManager create() { 1413cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick return IWindowManager.Stub.asInterface(ServiceManager.getService("window")); 1414cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick } 1415cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick }; 1416cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick 1417e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1418e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Enter a named critical span (e.g. an animation) 1419e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1420e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>The name is an arbitary label (or tag) that will be applied 1421e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * to any strictmode violation that happens while this span is 1422e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * active. You must call finish() on the span when done. 1423e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1424e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>This will never return null, but on devices without debugging 1425e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * enabled, this may return a dummy object on which the finish() 1426e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * method is a no-op. 1427e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1428e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>TODO: add CloseGuard to this, verifying callers call finish. 1429e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1430e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1431e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1432e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public static Span enterCriticalSpan(String name) { 14331181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick if (IS_USER_BUILD) { 14341181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick return NO_OP_SPAN; 14351181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 1436e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (name == null || name.isEmpty()) { 1437e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick throw new IllegalArgumentException("name must be non-null and non-empty"); 1438e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1439e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = sThisThreadSpanState.get(); 1440e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span span = null; 1441e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1442e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mFreeListHead != null) { 1443e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span = state.mFreeListHead; 1444e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListHead = span.mNext; 1445e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListSize--; 1446e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } else { 1447e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Shouldn't have to do this often. 1448e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span = new Span(state); 1449e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1450e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mName = name; 1451e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mCreateMillis = SystemClock.uptimeMillis(); 1452e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mNext = state.mActiveHead; 1453e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mPrev = null; 1454e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveHead = span; 1455e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveSize++; 1456e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (span.mNext != null) { 1457e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mNext.mPrev = span; 1458e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 14591cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize); 1460e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1461e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return span; 1462e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1463e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1464e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1465e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1466cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Parcelable that gets sent in Binder call headers back to callers 1467cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * to report violations that happened during a cross-process call. 1468cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 1469cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @hide 1470cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1471cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public static class ViolationInfo { 1472cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1473cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Stack and other stuff info. 1474cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1475cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final ApplicationErrorReport.CrashInfo crashInfo; 1476cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1477cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1478cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The strict mode policy mask at the time of violation. 1479cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1480cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final int policy; 1481cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1482cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1483cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The wall time duration of the violation, when known. -1 when 1484cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * not known. 1485cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1486cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int durationMillis = -1; 1487cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1488cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1489599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * The number of animations currently running. 1490599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick */ 1491599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick public int numAnimationsRunning = 0; 1492599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick 1493599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick /** 1494e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * List of tags from active Span instances during this 1495e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * violation, or null for none. 1496e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1497e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public String[] tags; 1498e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1499e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1500cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Which violation number this was (1-based) since the last Looper loop, 1501cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * from the perspective of the root caller (if it crossed any processes 1502cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * via Binder calls). The value is 0 if the root caller wasn't on a Looper 1503cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * thread. 1504cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1505cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int violationNumThisLoop; 1506cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1507cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1508cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The time (in terms of SystemClock.uptimeMillis()) that the 1509cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * violation occurred. 1510cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1511cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public long violationUptimeMillis; 1512cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1513cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1514bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * The action of the Intent being broadcast to somebody's onReceive 1515bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * on this thread right now, or null. 1516bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick */ 1517bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick public String broadcastIntentAction; 1518bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick 1519bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick /** 1520cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an uninitialized instance of ViolationInfo 1521cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1522cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo() { 1523cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = null; 1524cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = 0; 1525cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1526cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1527cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1528cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from an exception. 1529cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1530cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Throwable tr, int policy) { 1531cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(tr); 1532cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = SystemClock.uptimeMillis(); 1533cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this.policy = policy; 1534599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount(); 1535bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast(); 1536bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntent != null) { 1537bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = broadcastIntent.getAction(); 1538bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 1539e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = sThisThreadSpanState.get(); 1540e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1541e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int spanActiveCount = state.mActiveSize; 1542e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (spanActiveCount > MAX_SPAN_TAGS) { 1543e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick spanActiveCount = MAX_SPAN_TAGS; 1544e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1545e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (spanActiveCount != 0) { 1546e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.tags = new String[spanActiveCount]; 1547e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span iter = state.mActiveHead; 1548e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int index = 0; 1549e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick while (iter != null && index < spanActiveCount) { 1550e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.tags[index] = iter.mName; 1551e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick index++; 1552e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick iter = iter.mNext; 1553e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1554e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1555e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1556cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1557cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1558f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick @Override 1559f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick public int hashCode() { 1560f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick int result = 17; 1561f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + crashInfo.stackTrace.hashCode(); 1562f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (numAnimationsRunning != 0) { 1563f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result *= 37; 1564f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1565f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (broadcastIntentAction != null) { 1566f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + broadcastIntentAction.hashCode(); 1567f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1568f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (tags != null) { 1569f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick for (String tag : tags) { 1570f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + tag.hashCode(); 1571f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1572f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1573f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick return result; 1574f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1575f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick 1576cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1577cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 1578cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1579cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in) { 1580cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this(in, false); 1581cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1582cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1583cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1584cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 1585cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 1586cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @param unsetGatheringBit if true, the caller is the root caller 1587cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * and the gathering penalty should be removed. 1588cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1589cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in, boolean unsetGatheringBit) { 1590cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(in); 1591cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int rawPolicy = in.readInt(); 1592cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (unsetGatheringBit) { 1593cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy & ~PENALTY_GATHER; 1594cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } else { 1595cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy; 1596cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1597cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick durationMillis = in.readInt(); 1598cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationNumThisLoop = in.readInt(); 1599599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick numAnimationsRunning = in.readInt(); 1600cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = in.readLong(); 1601bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = in.readString(); 1602e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick tags = in.readStringArray(); 1603cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1604cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1605cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1606cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Save a ViolationInfo instance to a parcel. 1607cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1608cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void writeToParcel(Parcel dest, int flags) { 1609cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.writeToParcel(dest, flags); 1610cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(policy); 1611cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(durationMillis); 1612cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(violationNumThisLoop); 1613599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick dest.writeInt(numAnimationsRunning); 1614cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeLong(violationUptimeMillis); 1615bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick dest.writeString(broadcastIntentAction); 1616e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick dest.writeStringArray(tags); 1617cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1618cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1619cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1620cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1621cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Dump a ViolationInfo instance to a Printer. 1622cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1623cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void dump(Printer pw, String prefix) { 1624cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.dump(pw, prefix); 1625cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "policy: " + policy); 1626cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (durationMillis != -1) { 1627cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "durationMillis: " + durationMillis); 1628cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1629cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationNumThisLoop != 0) { 1630cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop); 1631cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1632599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick if (numAnimationsRunning != 0) { 1633599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning); 1634599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick } 1635cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis); 1636bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntentAction != null) { 1637bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction); 1638bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 1639e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (tags != null) { 1640e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int index = 0; 1641e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick for (String tag : tags) { 1642e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick pw.println(prefix + "tag[" + (index++) + "]: " + tag); 1643e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1644e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1645cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1646cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1647cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1648438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick} 1649