StrictMode.java revision f3d86be6d7d2999cd6bae236817688490df7da71
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; 22bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.content.Intent; 23438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.util.Log; 24cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrickimport android.util.Printer; 25cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrickimport android.util.Singleton; 266804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrickimport android.view.IWindowManager; 27438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 28438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport com.android.internal.os.RuntimeInit; 29438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 30438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport dalvik.system.BlockGuard; 31fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstromimport dalvik.system.CloseGuard; 32438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.PrintWriter; 345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.StringWriter; 355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.util.ArrayList; 3646d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap; 37bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrickimport java.util.concurrent.atomic.AtomicInteger; 3846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 39438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/** 4032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be 4132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix 4232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them. 4315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 4415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or 4515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI 4615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place. Keeping disk 4715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother, 489fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications. By keeping your application's main thread 499fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent 509fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a> 519fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users. 5215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 5315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is 5415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that 5515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency. It's often the case that 5615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be 5715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background 5815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes. If possible, it's best to assume that such 5915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p> 6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your 6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or 6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's 6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method: 6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre> 6715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() { 6815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * if (DEVELOPER_MODE) { 6932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}() 7032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskReads() 7132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskWrites() 7232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectNetwork() // or .detectAll() for all detectable problems 7332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 7432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 7532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}() 7662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * .detectLeakedSqlLiteObjects() 77fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * .detectLeakedClosableObjects() 7832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 7932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyDeath() 8032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 8115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 8215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * super.onCreate(); 8315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 8415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre> 8515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 8632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected. 8732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can 8832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your 8932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen. 9015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 9115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are 9215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler}, 9315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc. 9415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds. In particular, 9532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle. Use 9632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident. Network requests on the UI thread 9715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though. 9815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 9915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not 10015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses. While it does 10115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing 10215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best 10315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism. Notably, disk or network access from JNI calls 10415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it. Future versions of Android may catch 10515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode 10615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * enabled in shipping applications on the Android Market. 107438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 108438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode { 109438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static final String TAG = "StrictMode"; 11082829ef3b7c72bee36d8c17b36ac565f1856a310Brad Fitzpatrick private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE); 111438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1121181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); 1136804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick private static final boolean IS_ENG_BUILD = "eng".equals(Build.TYPE); 1141181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 11546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only log a duplicate stack trace to the logs every second. 11646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_LOG_INTERVAL_MS = 1000; 11746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 11846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only show an annoying dialog at most every 30 seconds 11946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_DIALOG_INTERVAL_MS = 30000; 12046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 121e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // How many Span tags (e.g. animations) to report. 122e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static final int MAX_SPAN_TAGS = 20; 123e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 124191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // How many offending stacks to keep track of (and time) per loop 125191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // of the Looper. 126191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final int MAX_OFFENSES_PER_LOOP = 10; 127191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 12832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Thread-policy: 129438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 13015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 13132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 13215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 13332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_WRITE = 0x01; // for ThreadPolicy 13415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 13515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 13632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 13715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 13832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_READ = 0x02; // for ThreadPolicy 13915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 14015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 14132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 14215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 14332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_NETWORK = 0x04; // for ThreadPolicy 144438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 14532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Process-policy: 146438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 147438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 14832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Note, a "VM_" bit, not thread. 14932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 15032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 15132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_VM_CURSOR_LEAKS = 0x200; // for ProcessPolicy 15232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 15332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 154fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Note, a "VM_" bit, not thread. 155fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 156fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 157fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400; // for ProcessPolicy 158fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 159fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 16032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 161438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 162438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_LOG = 0x10; // normal android.util.Log 163438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 16432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Used for both process and thread policy: 16532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 166438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 16732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 168438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 169438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DIALOG = 0x20; 170438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 171438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 172b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death on any detected violation. 173b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 17432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 175438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 176438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DEATH = 0x40; 177438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 178438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 179b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death just for detected network usage. 180b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 181b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 182b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 183b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static final int PENALTY_DEATH_ON_NETWORK = 0x200; 184b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 185b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 1866804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * Flash the screen during violations. 1876804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * 1886804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * @hide 1896804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick */ 1906804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick public static final int PENALTY_FLASH = 0x800; 1916804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 1926804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick /** 19332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 194438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 195438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DROPBOX = 0x80; 196438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 197727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 198727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Non-public penalty mode which overrides all the other penalty 199727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * bits and signals that we're in a Binder call and we should 200727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * ignore the other penalty bits and instead serialize back all 201727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * our offending stack traces to the caller to ultimately handle 202727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * in the originating process. 203727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * 204703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * This must be kept in sync with the constant in libs/binder/Parcel.cpp 205703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * 206727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * @hide 207727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 208727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick public static final int PENALTY_GATHER = 0x100; 209727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 21032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 21171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick * Mask of all the penalty bits. 21271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick */ 21371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick private static final int PENALTY_MASK = 214b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER | 2156804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH; 21671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 21771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick /** 21832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The current VmPolicy in effect. 21932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 22032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static volatile int sVmPolicyMask = 0; 22132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 222bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick /** 223bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * The number of threads trying to do an async dropbox write. 224bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * Just to limit ourselves out of paranoia. 225bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick */ 226bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0); 227bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 22832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private StrictMode() {} 22932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 23032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 23132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to a certain thread. 23232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 23332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setThreadPolicy}. The current policy 23432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can be retrieved with {@link #getThreadPolicy}. 23532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 23632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note that multiple penalties may be provided and they're run 23732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * in order from least to most severe (logging before process 23832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * death, for example). There's currently no mechanism to choose 23932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * different penalties for different detected actions. 24032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 24132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class ThreadPolicy { 24232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 24332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 24432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 24532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final ThreadPolicy LAX = new ThreadPolicy(0); 24632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 24732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 24832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 24932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private ThreadPolicy(int mask) { 25032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 25132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 25232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 25332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 25432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 25532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.ThreadPolicy; mask=" + mask + "]"; 25632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 25732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 25832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 25932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Creates ThreadPolicy instances. Methods whose names start 26032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 26132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 26232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 26332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 26432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 26532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 26632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 26732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 26832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 26932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 27032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() 27132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 27232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 27332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 27432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(policy); 27532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 27632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 27732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 27832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask = 0; 27932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 28032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 28132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Create a Builder that detects nothing and has no 28232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations. (but note that {@link #build} will default 28332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to enabling {@link #penaltyLog} if no other penalties 28432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * are specified) 28532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 28632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder() { 28732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = 0; 28832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 28932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 29032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 29132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Initialize a Builder from an existing ThreadPolicy. 29232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 29332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder(ThreadPolicy policy) { 29432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = policy.mask; 29532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 29632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 29732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 29832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 29932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 30032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>As of the Gingerbread release this includes network and 30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * disk operations but will likely expand in future releases. 30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK); 30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable the detection of everything. 30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitAll() { 31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK); 31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of network operations. 31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectNetwork() { 31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_NETWORK); 31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of network operations. 32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitNetwork() { 32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_NETWORK); 32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk reads. 33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskReads() { 33232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_READ); 33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk reads. 33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskReads() { 33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_READ); 34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk writes. 34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskWrites() { 34632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_WRITE); 34732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 34832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk writes. 35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskWrites() { 35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_WRITE); 35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Show an annoying dialog to the developer on detected 35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations, rate-limited to be only a little annoying. 35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDialog() { 36132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DIALOG); 36232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crash the whole process on violation. This penalty runs at 36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so you'll still get 36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * see logging or other violations before the process dies. 368b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 369b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies 370b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * to disk reads, disk writes, and network usage if their 371b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * corresponding detect flags are set. 37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 37632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 37732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 378b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Crash the whole process on any network usage. Unlike 379b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * {@link #penaltyDeath}, this penalty runs 380b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <em>before</em> anything else. You must still have 381b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * called {@link #detectNetwork} to enable this. 382b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 383b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>In the Honeycomb or later SDKs, this is on by default. 384b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 385b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public Builder penaltyDeathOnNetwork() { 386b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick return enable(PENALTY_DEATH_ON_NETWORK); 387b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 388b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 389b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 3906804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * Flash the screen during a violation. 3916804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick */ 3926804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick public Builder penaltyFlashScreen() { 3936804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick return enable(PENALTY_FLASH); 3946804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 3956804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 3966804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick /** 39732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 39832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 39932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 40032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 40132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 40232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 40332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 40432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 40532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 40632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 40732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 41032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 41132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 41232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 41332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 41432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 41532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 41632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 41732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 41832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder disable(int bit) { 41932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask &= ~bit; 42032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 42132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 42232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 42332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 42432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the ThreadPolicy instance. 42532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 42632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 42732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 42832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 42932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public ThreadPolicy build() { 43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 43332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 43432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 43532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(mMask); 43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 44332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 44432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to all threads in the virtual machine's process. 44532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 44632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setVmPolicy}. 44732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 44832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class VmPolicy { 44932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 45032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 45132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 45232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final VmPolicy LAX = new VmPolicy(0); 45332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 45432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 45532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 45632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private VmPolicy(int mask) { 45732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 45832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 45932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 46032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 46132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 46232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.VmPolicy; mask=" + mask + "]"; 46332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 46432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 46532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 46632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Creates {@link VmPolicy} instances. Methods whose names start 46732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 46832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 46932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 47032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 47132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 47232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 47332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 47432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 47532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 47632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 47732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() 47832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 47932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 48032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 48132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(policy); 48232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 48332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 48432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask; 48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 490fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>In the Honeycomb release this includes leaks of 491fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * SQLite cursors and other closable objects but will 492fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * likely expand in future releases. 49332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 495fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return enable(DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS); 49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 49832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 49932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect when an 50032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link android.database.sqlite.SQLiteCursor} or other 50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * SQLite object is finalized without having been closed. 50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 50332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You always want to explicitly close your SQLite 50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * cursors to avoid unnecessary database contention and 50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * temporary memory leaks. 50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectLeakedSqlLiteObjects() { 50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_VM_CURSOR_LEAKS); 50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 512fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Detect when an {@link java.io.Closeable} or other 513fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * object with a explict termination method is finalized 514fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * without having been closed. 515fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * 516fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>You always want to explicitly close such objects to 517fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * avoid unnecessary resources leaks. 518fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 519fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public Builder detectLeakedClosableObjects() { 520fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return enable(DETECT_VM_CLOSABLE_LEAKS); 521fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 522fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 523fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 52432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crashes the whole process on violation. This penalty runs at 52532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so yo you'll still get 52632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * your logging or other violations before the process dies. 52732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 52832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 52932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 53032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 53132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 53332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 53832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 53932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 54032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 54232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 54332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 54432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 54532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 54632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 54732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 54832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 54932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 55032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 55132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 55232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the VmPolicy instance. 55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 55832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 55932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 56032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 56132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public VmPolicy build() { 56232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 56332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 56432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 56532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 56632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 56732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 56832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 56932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new VmPolicy(mMask); 57032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 57132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 57232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 573438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 574438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 5755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Log of strict mode violation stack traces that have occurred 5765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * during a Binder call, to be serialized back later to the caller 5775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * via Parcel.writeNoException() (amusingly) where the caller can 5785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * choose how to react. 5795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 580cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations = 581cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 582cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 583703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // Starts null to avoid unnecessary allocations when 584703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // checking whether there are any violations or not in 585703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // hasGatheredViolations() below. 586703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return null; 5875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 5885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick }; 5895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 5905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 59132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions on the current thread should 59232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * be detected, as well as the penalty if such actions occur. 59315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 59432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Internally this sets a thread-local variable which is 59515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagated across cross-process IPC calls, meaning you can 59615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * catch violations when a system service or another process 59715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * accesses the disk or network on your behalf. 598438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 59932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 600438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 60132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setThreadPolicy(final ThreadPolicy policy) { 60232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(policy.mask); 60332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 60432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 60532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static void setThreadPolicyMask(final int policyMask) { 606727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // In addition to the Java-level thread-local in Dalvik's 607727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // BlockGuard, we also need to keep a native thread-local in 608727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Binder in order to propagate the value across Binder calls, 609727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // even across native-only processes. The two are kept in 610727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // sync via the callback to onStrictModePolicyChange, below. 611727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(policyMask); 612727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 613727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // And set the Android native version... 614727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick Binder.setThreadStrictModePolicy(policyMask); 615727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 616727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 617727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Sets the policy in Dalvik/libcore (BlockGuard) 618727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void setBlockGuardPolicy(final int policyMask) { 61946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick if (policyMask == 0) { 62046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY); 62146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick return; 62246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 623438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 624438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick if (!(policy instanceof AndroidBlockGuardPolicy)) { 625438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick BlockGuard.setThreadPolicy(new AndroidBlockGuardPolicy(policyMask)); 626438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 627438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick AndroidBlockGuardPolicy androidPolicy = (AndroidBlockGuardPolicy) policy; 628438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick androidPolicy.setPolicyMask(policyMask); 629438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 630438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 631438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 6324b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom // Sets up CloseGuard in Dalvik/libcore 6334b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom private static void setCloseGuardEnabled(boolean enabled) { 6347c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) { 6354b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom CloseGuard.setReporter(new AndroidCloseGuardReporter()); 6364b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 6374b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom CloseGuard.setEnabled(enabled); 6384b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 6394b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 6405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeNetworkViolation extends BlockGuard.BlockGuardPolicyException { 6415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeNetworkViolation(int policyMask) { 64232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_NETWORK); 6435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6445b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 6465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeDiskReadViolation extends BlockGuard.BlockGuardPolicyException { 6475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskReadViolation(int policyMask) { 64832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_DISK_READ); 6495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6505b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 6525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeDiskWriteViolation extends BlockGuard.BlockGuardPolicyException { 6535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskWriteViolation(int policyMask) { 65432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_DISK_WRITE); 6555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 658438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 65915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * Returns the bitmask of the current thread's policy. 660438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 66132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled 66232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 66332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 664438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 66532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static int getThreadPolicyMask() { 666438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return BlockGuard.getThreadPolicy().getPolicyMask(); 667438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 668438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 6695b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 67032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Returns the current thread's policy. 67132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 67232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy getThreadPolicy() { 67332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(getThreadPolicyMask()); 67432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 67532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 67632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 67732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 67832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 67932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit both disk reads & writes, and sets the new policy 68032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 68132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 68297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 68332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to {@link #setThreadPolicy} to 68432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * restore the policy at the end of a block 68597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 68632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskWrites() { 68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ); 68932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 69032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 69197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 69232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 69397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 69497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 69597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 69632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 69732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 69832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit disk reads, and sets the new policy 69932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 70032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 70197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 70232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to setThreadPolicy to 70397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * restore the policy. 70497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 70532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskReads() { 70632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 70732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ); 70832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 70932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 71097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 71132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 71297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 71397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 71497461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 71550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * Enable DropBox logging for debug phone builds. 71650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * 71750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * @hide 71850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick */ 71950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick public static boolean conditionallyEnableDebugLogging() { 72050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // For debug builds, log event loop stalls to dropbox for analysis. 72150d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // Similar logic also appears in ActivityThread.java for system apps. 7221181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick if (IS_USER_BUILD) { 7237c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick setCloseGuardEnabled(false); 72450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return false; 72550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 72632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.setThreadPolicyMask( 72732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.DETECT_DISK_WRITE | 72832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.DETECT_DISK_READ | 72932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick StrictMode.DETECT_NETWORK | 7306804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick StrictMode.PENALTY_DROPBOX | 7316804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick (IS_ENG_BUILD ? StrictMode.PENALTY_FLASH : 0) 7326804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick ); 73332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick sVmPolicyMask = StrictMode.DETECT_VM_CURSOR_LEAKS | 734fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom StrictMode.DETECT_VM_CLOSABLE_LEAKS | 735d188ededa6f61dbe5a700e81db22c02478ce60ccBrad Fitzpatrick StrictMode.PENALTY_DROPBOX; 7367c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); 73750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return true; 73850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 73950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick 74050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick /** 741b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Used by the framework to make network usage on the main 742b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * thread a fatal error. 743b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 744b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 745b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 746b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static void enableDeathOnNetwork() { 747b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int oldPolicy = getThreadPolicyMask(); 748b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK; 749b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick setThreadPolicyMask(newPolicy); 750b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 751b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 752b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 7535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Parses the BlockGuard policy mask out from the Exception's 7545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * getMessage() String value. Kinda gross, but least 7555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * invasive. :/ 7565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 7575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Input is of form "policy=137 violation=64" 7585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 7595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Returns 0 on failure, which is a valid policy, but not a 7605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * valid policy during a violation (else there must've been 7615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * some policy in effect to violate). 7625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 7635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parsePolicyFromMessage(String message) { 7645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null || !message.startsWith("policy=")) { 7655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int spaceIndex = message.indexOf(' '); 7685b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (spaceIndex == -1) { 7695b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7715b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String policyString = message.substring(7, spaceIndex); 7725b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 7735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(policyString).intValue(); 7745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 7755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 7795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 7805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Like parsePolicyFromMessage(), but returns the violation. 7815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 7825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parseViolationFromMessage(String message) { 7835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null) { 7845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int violationIndex = message.indexOf("violation="); 7875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (violationIndex == -1) { 7885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7905b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String violationString = message.substring(violationIndex + 10); 7915b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 7925b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(violationString).intValue(); 7935b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 7945b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 7955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 7975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 798191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed = 799191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 800191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 801191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return new ArrayList<ViolationInfo>(); 802191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 803191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick }; 804191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 805191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static boolean tooManyViolationsThisLoop() { 806191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP; 807191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 808191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 809438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static class AndroidBlockGuardPolicy implements BlockGuard.Policy { 810438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private int mPolicyMask; 81146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 81246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Map from violation stacktrace hashcode -> uptimeMillis of 81346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // last violation. No locking needed, as this is only 81446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // accessed by the same thread. 81546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private final HashMap<Integer, Long> mLastViolationTime = new HashMap<Integer, Long>(); 816438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 817438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public AndroidBlockGuardPolicy(final int policyMask) { 818438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 819438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 820438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 8215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick @Override 8225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public String toString() { 8235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask; 8245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 826438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 827438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public int getPolicyMask() { 828438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return mPolicyMask; 829438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 830438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 831438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 832438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onWriteToDisk() { 83332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_WRITE) == 0) { 834438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 835438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 836191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 837191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 838191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 839cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask); 840cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 841cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 842438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 843438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 844438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 845438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onReadFromDisk() { 84632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_READ) == 0) { 847438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 848438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 849191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 850191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 851191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 852cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask); 853cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 854cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 855438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 856438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 857438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 858438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onNetwork() { 85932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_NETWORK) == 0) { 860438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 861438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 862b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) { 863b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick throw new NetworkOnMainThreadException(); 864b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 865191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 866191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 867191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 868cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask); 869cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 870cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 871438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 872438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 873438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void setPolicyMask(int policyMask) { 874438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 875438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 876438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 8775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Start handling a violation that just started and hasn't 8785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // actually run yet (e.g. no disk write or network operation 8795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // has yet occurred). This sees if we're in an event loop 8805b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread and, if so, uses it to roughly measure how long the 8815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation took. 8825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) { 883cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ViolationInfo info = new ViolationInfo(e, e.getPolicy()); 884cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.violationUptimeMillis = SystemClock.uptimeMillis(); 885cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolationWithTimingAttempt(info); 886cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 887438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 888cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Attempts to fill in the provided ViolationInfo's 889cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // durationMillis field if this thread has a Looper we can use 890cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // to measure with. We measure from the time of violation 891cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // until the time the looper is idle again (right before 892cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // the next epoll_wait) 893cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolationWithTimingAttempt(final ViolationInfo info) { 894438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick Looper looper = Looper.myLooper(); 895cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 896cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Without a Looper, we're unable to time how long the 897cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // violation takes place. This case should be rare, as 898cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // most users will care about timing violations that 899cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // happen on their main UI thread. Note that this case is 900cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // also hit when a violation takes place in a Binder 901cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // thread, in "gather" mode. In this case, the duration 902cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // of the violation is computed by the ultimate caller and 903cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // its Looper, if any. 904cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // TODO: if in gather mode, ignore Looper.myLooper() and always 905cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // go into this immediate mode? 906438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick if (looper == null) { 907cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis = -1; // unknown (redundant, already set) 908cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(info); 909cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 910438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 911438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 912cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick MessageQueue queue = Looper.myQueue(); 913cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ArrayList<ViolationInfo> records = violationsBeingTimed.get(); 914191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (records.size() >= MAX_OFFENSES_PER_LOOP) { 915cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Not worth measuring. Too many offenses in one loop. 916cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 917cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 918cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.add(info); 919cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (records.size() > 1) { 920cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // There's already been a violation this loop, so we've already 921cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // registered an idle handler to process the list of violations 922cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // at the end of this Looper's loop. 923cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 924cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 925cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 9266804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ? 927cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick sWindowManager.get() : null; 9286804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick if (windowManager != null) { 9296804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick try { 9306804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick windowManager.showStrictModeViolation(true); 9316804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } catch (RemoteException unused) { 9326804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 9336804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 9346804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 935cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick queue.addIdleHandler(new MessageQueue.IdleHandler() { 936cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public boolean queueIdle() { 937cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick long loopFinishTime = SystemClock.uptimeMillis(); 938cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (int n = 0; n < records.size(); ++n) { 939cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo v = records.get(n); 940cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.violationNumThisLoop = n + 1; 941cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.durationMillis = 942cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick (int) (loopFinishTime - v.violationUptimeMillis); 943cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(v); 944cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 945cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.clear(); 9466804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick if (windowManager != null) { 9476804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick try { 9486804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick windowManager.showStrictModeViolation(false); 9496804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } catch (RemoteException unused) { 9506804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 9516804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 952cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return false; // remove this idle handler from the array 953cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 954cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick }); 9555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 956438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 9575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Note: It's possible (even quite likely) that the 9585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread-local policy mask has changed from the time the 9595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation fired and now (after the violating code ran) due 9605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // to people who push/pop temporary policy in regions of code, 9615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // hence the policy being passed around. 962cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolation(final ViolationInfo info) { 963cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) { 964cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.wtf(TAG, "unexpected null stacktrace"); 9655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 9665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 967438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 968cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy); 96946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 970cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_GATHER) != 0) { 971cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 972703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 973cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations = new ArrayList<ViolationInfo>(1); 974703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(violations); 975703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else if (violations.size() >= 5) { 9765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Too many. In a loop or something? Don't gather them all. 9775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 9785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 979cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (ViolationInfo previous : violations) { 980cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) { 9815b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Duplicate. Don't log. 9825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 9835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 9845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 985cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations.add(info); 986727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick return; 987727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 988727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 98946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Not perfect, but fast and good enough for dup suppression. 990f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick Integer crashFingerprint = info.hashCode(); 99146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long lastViolationTime = 0; 99246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick if (mLastViolationTime.containsKey(crashFingerprint)) { 99346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick lastViolationTime = mLastViolationTime.get(crashFingerprint); 99446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 99546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long now = SystemClock.uptimeMillis(); 99646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick mLastViolationTime.put(crashFingerprint, now); 99746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long timeSinceLastViolationMillis = lastViolationTime == 0 ? 99846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Long.MAX_VALUE : (now - lastViolationTime); 99946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1000cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_LOG) != 0 && 100146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { 1002cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.durationMillis != -1) { 10035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation; ~duration=" + 1004cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis + " ms: " + info.crashInfo.stackTrace); 1005438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 1006cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace); 1007438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1008438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1009438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 101071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // The violationMaskSubset, passed to ActivityManager, is a 101146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // subset of the original StrictMode policy bitmask, with 101246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // only the bit violated and penalty bits to be executed 101346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // by the ActivityManagerService remaining set. 1014cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationMaskSubset = 0; 101546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1016cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DIALOG) != 0 && 101746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) { 1018cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DIALOG; 101946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 102046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1021cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) { 1022cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DROPBOX; 102346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 102446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1025cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationMaskSubset != 0) { 1026cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage); 1027cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= violationBit; 102832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 102971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 103071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick final boolean justDropBox = (info.policy & PENALTY_MASK) == PENALTY_DROPBOX; 103171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick if (justDropBox) { 103271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // If all we're going to ask the activity manager 103371678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // to do is dropbox it (the common case during 103471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // platform development), we can avoid doing this 103571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // call synchronously which Binder data suggests 103671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // isn't always super fast, despite the implementation 103771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // in the ActivityManager trying to be mostly async. 1038bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick dropboxViolationAsync(violationMaskSubset, info); 103971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick return; 104071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick } 104171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 104271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // Normal synchronous call to the ActivityManager. 1043438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick try { 1044727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 1045727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 1046727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 1047727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // We restore the current policy below, in the finally block. 104832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 1049727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 1050438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 1051438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick RuntimeInit.getApplicationObject(), 1052cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset, 1053cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info); 1054438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } catch (RemoteException e) { 105546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 1056727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } finally { 1057727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Restore the policy. 105832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 1059438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1060438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1061438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1062cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DEATH) != 0) { 1063438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick System.err.println("StrictMode policy violation with POLICY_DEATH; shutting down."); 1064438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick Process.killProcess(Process.myPid()); 1065438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick System.exit(10); 1066438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1067438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1068438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1069727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 1070bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick /** 1071bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * In the common case, as set by conditionallyEnableDebugLogging, 1072bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * we're just dropboxing any violations but not showing a dialog, 1073bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * not loggging, and not killing the process. In these cases we 1074bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * don't need to do a synchronous call to the ActivityManager. 1075bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * This is used by both per-thread and vm-wide violations when 1076bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * applicable. 1077bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick */ 1078bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick private static void dropboxViolationAsync( 1079bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick final int violationMaskSubset, final ViolationInfo info) { 1080bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int outstanding = sDropboxCallsInFlight.incrementAndGet(); 1081bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (outstanding > 20) { 1082bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // What's going on? Let's not make make the situation 1083bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // worse and just not log. 1084bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick sDropboxCallsInFlight.decrementAndGet(); 1085bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick return; 1086bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1087bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1088bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding); 1089bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1090bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick new Thread("callActivityManagerForStrictModeDropbox") { 1091bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick public void run() { 1092bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 1093bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick try { 1094bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick ActivityManagerNative.getDefault(). 1095bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick handleApplicationStrictModeViolation( 1096bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick RuntimeInit.getApplicationObject(), 1097bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick violationMaskSubset, 1098bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick info); 1099bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } catch (RemoteException e) { 1100bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick Log.e(TAG, "RemoteException handling StrictMode violation", e); 1101bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1102bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int outstanding = sDropboxCallsInFlight.decrementAndGet(); 1103bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding); 1104bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1105bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick }.start(); 1106bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1107bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 11084b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom private static class AndroidCloseGuardReporter implements CloseGuard.Reporter { 11094b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom public void report (String message, Throwable allocationSite) { 11104b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom onVmPolicyViolation(message, allocationSite); 11114b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 11124b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 11134b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 1114727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 11155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 11165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 11175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static boolean hasGatheredViolations() { 1118703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return gatheredViolations.get() != null; 1119703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1120703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick 1121703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /** 1122703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Called from Parcel.writeException(), so we drop this memory and 1123703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * don't incorrectly attribute it to the wrong caller on the next 1124703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Binder call on this thread. 1125703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick */ 1126703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /* package */ static void clearGatheredViolations() { 1127703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 11285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 11295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 11305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 113132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions in the VM process (on any 113232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * thread) should be detected, as well as the penalty if such 113332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * actions occur. 113432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 113532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 113632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 113732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setVmPolicy(final VmPolicy policy) { 113832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick sVmPolicyMask = policy.mask; 11394b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); 114032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 114132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 114232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 114332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Gets the current VM policy. 114432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 114532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static VmPolicy getVmPolicy() { 114632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new VmPolicy(sVmPolicyMask); 114732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 114832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 114932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 115062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * Enable the recommended StrictMode defaults, with violations just being logged. 115162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * 115262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * <p>This catches disk and network access on the main thread, as 1153fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * well as leaked SQLite cursors and unclosed resources. This is 1154fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * simply a wrapper around {@link #setVmPolicy} and {@link 1155fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * #setThreadPolicy}. 115662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick */ 115762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick public static void enableDefaults() { 115862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 115962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .detectAll() 116062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 116162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 116262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 116362a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .detectLeakedSqlLiteObjects() 1164fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom .detectLeakedClosableObjects() 116562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 116662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 116762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick } 116862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick 116962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick /** 117032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 117132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 117232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static boolean vmSqliteObjectLeaksEnabled() { 117332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0; 117432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 117532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 117632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 117732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 117832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 1179fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public static boolean vmClosableObjectLeaksEnabled() { 1180fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0; 1181fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 1182fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 1183fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 1184fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 1185fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 118632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void onSqliteObjectLeaked(String message, Throwable originStack) { 11874b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom onVmPolicyViolation(message, originStack); 11884b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 11894b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 11904b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom /** 11914b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom * @hide 11924b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom */ 11934b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom public static void onVmPolicyViolation(String message, Throwable originStack) { 119432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((sVmPolicyMask & PENALTY_LOG) != 0) { 119532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Log.e(TAG, message, originStack); 119632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 119732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1198bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0; 1199bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0; 1200bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1201bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int violationMaskSubset = PENALTY_DROPBOX | DETECT_VM_CURSOR_LEAKS; 1202bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask); 1203bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1204bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDropbox && !penaltyDeath) { 1205bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // Common case for userdebug/eng builds. If no death and 1206bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // just dropboxing, we can do the ActivityManager call 1207bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // asynchronously. 1208bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick dropboxViolationAsync(violationMaskSubset, info); 1209bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick return; 1210bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 121132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1212bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDropbox) { 121332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // The violationMask, passed to ActivityManager, is a 121432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // subset of the original StrictMode policy bitmask, with 121532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // only the bit violated and penalty bits to be executed 121632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // by the ActivityManagerService remaining set. 121732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 121832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick try { 121932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 122032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 122132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 122232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // We restore the current policy below, in the finally block. 122332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 122432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 122532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 122632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick RuntimeInit.getApplicationObject(), 122732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick violationMaskSubset, 122832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick info); 122932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } catch (RemoteException e) { 123032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 123132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } finally { 123232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Restore the policy. 123332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 123432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 123532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 123632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1237bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDeath) { 123832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down."); 123932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Process.killProcess(Process.myPid()); 124032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.exit(10); 124132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 124232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 124332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 124432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 12455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 12465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 12475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void writeGatheredViolationsToParcel(Parcel p) { 1248cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 1249703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 1250703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(0); 1251703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else { 1252703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(violations.size()); 1253703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick for (int i = 0; i < violations.size(); ++i) { 1254703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.get(i).writeToParcel(p, 0 /* unused flags? */); 1255703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1256703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size()); 1257703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.clear(); // somewhat redundant, as we're about to null the threadlocal 12585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1259703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 12605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 12615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 12625b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class LogStackTrace extends Exception {} 12635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 12645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 12655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS, 12665b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * we here read back all the encoded violations. 12675b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 12685b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void readAndHandleBinderCallViolations(Parcel p) { 12695b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Our own stack trace to append 12705b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick StringWriter sw = new StringWriter(); 1271cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new LogStackTrace().printStackTrace(new PrintWriter(sw)); 12725b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String ourStack = sw.toString(); 12735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 127432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int policyMask = getThreadPolicyMask(); 1275cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0; 12765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 12775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int numViolations = p.readInt(); 12785b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick for (int i = 0; i < numViolations; ++i) { 12795b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call. i=" + i); 1280cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo info = new ViolationInfo(p, !currentlyGathering); 1281cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack; 12825b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 12835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (policy instanceof AndroidBlockGuardPolicy) { 1284cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info); 12855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 12865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 12875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 12885b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 12895b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 1290727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Called from android_util_Binder.cpp's 1291727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * android_os_Parcel_enforceInterface when an incoming Binder call 1292727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * requires changing the StrictMode policy mask. The role of this 1293727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * function is to ask Binder for its current (native) thread-local 1294727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * policy value and synchronize it to libcore's (Java) 1295727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * thread-local policy value. 1296727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 1297727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void onBinderStrictModePolicyChange(int newPolicy) { 1298727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(newPolicy); 1299727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 1300cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1301cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1302e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * A tracked, critical time span. (e.g. during an animation.) 1303e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1304e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * The object itself is a linked list node, to avoid any allocations 1305e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * during rapid span entries and exits. 1306e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1307e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1308e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1309e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public static class Span { 1310e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private String mName; 1311e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private long mCreateMillis; 1312e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private Span mNext; 1313e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private Span mPrev; // not used when in freeList, only active 1314e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private final ThreadSpanState mContainerState; 1315e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1316e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span(ThreadSpanState threadState) { 1317e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mContainerState = threadState; 1318e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1319e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 13201181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // Empty constructor for the NO_OP_SPAN 13211181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick protected Span() { 13221181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick mContainerState = null; 13231181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 13241181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 1325e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1326e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * To be called when the critical span is complete (i.e. the 1327e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * animation is done animating). This can be called on any 1328e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * thread (even a different one from where the animation was 1329e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * taking place), but that's only a defensive implementation 1330e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * measure. It really makes no sense for you to call this on 1331e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * thread other than that where you created it. 1332e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1333e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1334e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1335e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public void finish() { 1336e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = mContainerState; 1337e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1338e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mName == null) { 1339e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Duplicate finish call. Ignore. 1340e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return; 1341e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1342e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1343e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Remove ourselves from the active list. 1344e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mPrev != null) { 1345e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mPrev.mNext = mNext; 1346e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1347e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mNext != null) { 1348e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mNext.mPrev = mPrev; 1349e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1350e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mActiveHead == this) { 1351e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveHead = mNext; 1352e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1353e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 13541cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick state.mActiveSize--; 13551cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick 13561cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize); 13571cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick 1358e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mCreateMillis = -1; 1359e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mName = null; 1360e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mPrev = null; 1361e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mNext = null; 1362e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1363e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Add ourselves to the freeList, if it's not already 1364e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // too big. 1365e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mFreeListSize < 5) { 1366e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mNext = state.mFreeListHead; 1367e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListHead = this; 1368e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListSize++; 1369e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1370e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1371e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1372e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1373e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 13741181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // The no-op span that's used in user builds. 13751181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick private static final Span NO_OP_SPAN = new Span() { 13761181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick public void finish() { 13771181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // Do nothing. 13781181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 13791181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick }; 13801181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 1381e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1382e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Linked lists of active spans and a freelist. 1383e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1384e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Locking notes: there's one of these structures per thread and 1385e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * all members of this structure (as well as the Span nodes under 1386e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * it) are guarded by the ThreadSpanState object instance. While 1387e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * in theory there'd be no locking required because it's all local 1388e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * per-thread, the finish() method above is defensive against 1389e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * people calling it on a different thread from where they created 1390e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * the Span, hence the locking. 1391e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1392e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static class ThreadSpanState { 1393e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public Span mActiveHead; // doubly-linked list. 1394e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public int mActiveSize; 1395e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public Span mFreeListHead; // singly-linked list. only changes at head. 1396e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public int mFreeListSize; 1397e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1398e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1399e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState = 1400e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick new ThreadLocal<ThreadSpanState>() { 1401e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick @Override protected ThreadSpanState initialValue() { 1402e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return new ThreadSpanState(); 1403e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1404e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick }; 1405e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1406cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() { 1407cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick protected IWindowManager create() { 1408cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick return IWindowManager.Stub.asInterface(ServiceManager.getService("window")); 1409cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick } 1410cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick }; 1411cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick 1412e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1413e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Enter a named critical span (e.g. an animation) 1414e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1415e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>The name is an arbitary label (or tag) that will be applied 1416e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * to any strictmode violation that happens while this span is 1417e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * active. You must call finish() on the span when done. 1418e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1419e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>This will never return null, but on devices without debugging 1420e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * enabled, this may return a dummy object on which the finish() 1421e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * method is a no-op. 1422e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1423e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>TODO: add CloseGuard to this, verifying callers call finish. 1424e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1425e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1426e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1427e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public static Span enterCriticalSpan(String name) { 14281181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick if (IS_USER_BUILD) { 14291181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick return NO_OP_SPAN; 14301181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 1431e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (name == null || name.isEmpty()) { 1432e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick throw new IllegalArgumentException("name must be non-null and non-empty"); 1433e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1434e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = sThisThreadSpanState.get(); 1435e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span span = null; 1436e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1437e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mFreeListHead != null) { 1438e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span = state.mFreeListHead; 1439e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListHead = span.mNext; 1440e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListSize--; 1441e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } else { 1442e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Shouldn't have to do this often. 1443e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span = new Span(state); 1444e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1445e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mName = name; 1446e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mCreateMillis = SystemClock.uptimeMillis(); 1447e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mNext = state.mActiveHead; 1448e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mPrev = null; 1449e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveHead = span; 1450e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveSize++; 1451e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (span.mNext != null) { 1452e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mNext.mPrev = span; 1453e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 14541cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize); 1455e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1456e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return span; 1457e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1458e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1459e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1460e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1461cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Parcelable that gets sent in Binder call headers back to callers 1462cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * to report violations that happened during a cross-process call. 1463cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 1464cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @hide 1465cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1466cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public static class ViolationInfo { 1467cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1468cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Stack and other stuff info. 1469cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1470cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final ApplicationErrorReport.CrashInfo crashInfo; 1471cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1472cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1473cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The strict mode policy mask at the time of violation. 1474cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1475cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final int policy; 1476cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1477cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1478cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The wall time duration of the violation, when known. -1 when 1479cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * not known. 1480cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1481cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int durationMillis = -1; 1482cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1483cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1484599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * The number of animations currently running. 1485599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick */ 1486599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick public int numAnimationsRunning = 0; 1487599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick 1488599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick /** 1489e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * List of tags from active Span instances during this 1490e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * violation, or null for none. 1491e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1492e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public String[] tags; 1493e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1494e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1495cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Which violation number this was (1-based) since the last Looper loop, 1496cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * from the perspective of the root caller (if it crossed any processes 1497cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * via Binder calls). The value is 0 if the root caller wasn't on a Looper 1498cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * thread. 1499cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1500cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int violationNumThisLoop; 1501cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1502cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1503cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The time (in terms of SystemClock.uptimeMillis()) that the 1504cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * violation occurred. 1505cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1506cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public long violationUptimeMillis; 1507cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1508cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1509bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * The action of the Intent being broadcast to somebody's onReceive 1510bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * on this thread right now, or null. 1511bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick */ 1512bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick public String broadcastIntentAction; 1513bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick 1514bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick /** 1515cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an uninitialized instance of ViolationInfo 1516cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1517cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo() { 1518cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = null; 1519cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = 0; 1520cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1521cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1522cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1523cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from an exception. 1524cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1525cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Throwable tr, int policy) { 1526cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(tr); 1527cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = SystemClock.uptimeMillis(); 1528cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this.policy = policy; 1529599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount(); 1530bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast(); 1531bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntent != null) { 1532bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = broadcastIntent.getAction(); 1533bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 1534e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = sThisThreadSpanState.get(); 1535e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1536e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int spanActiveCount = state.mActiveSize; 1537e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (spanActiveCount > MAX_SPAN_TAGS) { 1538e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick spanActiveCount = MAX_SPAN_TAGS; 1539e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1540e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (spanActiveCount != 0) { 1541e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.tags = new String[spanActiveCount]; 1542e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span iter = state.mActiveHead; 1543e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int index = 0; 1544e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick while (iter != null && index < spanActiveCount) { 1545e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.tags[index] = iter.mName; 1546e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick index++; 1547e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick iter = iter.mNext; 1548e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1549e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1550e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1551cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1552cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1553f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick @Override 1554f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick public int hashCode() { 1555f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick int result = 17; 1556f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + crashInfo.stackTrace.hashCode(); 1557f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (numAnimationsRunning != 0) { 1558f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result *= 37; 1559f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1560f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (broadcastIntentAction != null) { 1561f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + broadcastIntentAction.hashCode(); 1562f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1563f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (tags != null) { 1564f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick for (String tag : tags) { 1565f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + tag.hashCode(); 1566f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1567f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1568f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick return result; 1569f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1570f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick 1571cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1572cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 1573cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1574cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in) { 1575cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this(in, false); 1576cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1577cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1578cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1579cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 1580cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 1581cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @param unsetGatheringBit if true, the caller is the root caller 1582cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * and the gathering penalty should be removed. 1583cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1584cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in, boolean unsetGatheringBit) { 1585cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(in); 1586cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int rawPolicy = in.readInt(); 1587cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (unsetGatheringBit) { 1588cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy & ~PENALTY_GATHER; 1589cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } else { 1590cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy; 1591cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1592cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick durationMillis = in.readInt(); 1593cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationNumThisLoop = in.readInt(); 1594599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick numAnimationsRunning = in.readInt(); 1595cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = in.readLong(); 1596bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = in.readString(); 1597e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick tags = in.readStringArray(); 1598cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1599cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1600cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1601cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Save a ViolationInfo instance to a parcel. 1602cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1603cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void writeToParcel(Parcel dest, int flags) { 1604cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.writeToParcel(dest, flags); 1605cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(policy); 1606cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(durationMillis); 1607cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(violationNumThisLoop); 1608599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick dest.writeInt(numAnimationsRunning); 1609cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeLong(violationUptimeMillis); 1610bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick dest.writeString(broadcastIntentAction); 1611e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick dest.writeStringArray(tags); 1612cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1613cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1614cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1615cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1616cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Dump a ViolationInfo instance to a Printer. 1617cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1618cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void dump(Printer pw, String prefix) { 1619cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.dump(pw, prefix); 1620cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "policy: " + policy); 1621cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (durationMillis != -1) { 1622cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "durationMillis: " + durationMillis); 1623cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1624cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationNumThisLoop != 0) { 1625cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop); 1626cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1627599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick if (numAnimationsRunning != 0) { 1628599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning); 1629599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick } 1630cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis); 1631bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntentAction != null) { 1632bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction); 1633bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 1634e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (tags != null) { 1635e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int index = 0; 1636e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick for (String tag : tags) { 1637e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick pw.println(prefix + "tag[" + (index++) + "]: " + tag); 1638e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1639e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1640cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1641cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1642cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1643438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick} 1644