StrictMode.java revision bea168c09d173fb99cfc91c562c4a497a5e7d2d2
1438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/* 2438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Copyright (C) 2010 The Android Open Source Project 3438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 4438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Licensed under the Apache License, Version 2.0 (the "License"); 5438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * you may not use this file except in compliance with the License. 6438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * You may obtain a copy of the License at 7438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 8438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * http://www.apache.org/licenses/LICENSE-2.0 9438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 10438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * Unless required by applicable law or agreed to in writing, software 11438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * distributed under the License is distributed on an "AS IS" BASIS, 12438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * See the License for the specific language governing permissions and 14438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * limitations under the License. 15438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 16438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpackage android.os; 17438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 18599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrickimport android.animation.ValueAnimator; 19438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.app.ActivityManagerNative; 20bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.app.ActivityThread; 21438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.app.ApplicationErrorReport; 221065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrickimport android.app.IActivityManager; 23bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrickimport android.content.Intent; 24438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport android.util.Log; 25cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrickimport android.util.Printer; 26cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrickimport android.util.Singleton; 276804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrickimport android.view.IWindowManager; 28438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 29438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport com.android.internal.os.RuntimeInit; 30438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 31438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickimport dalvik.system.BlockGuard; 32fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstromimport dalvik.system.CloseGuard; 33438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.PrintWriter; 355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.io.StringWriter; 365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrickimport java.util.ArrayList; 3746d42387464a651268648659e91d022566d4844cBrad Fitzpatrickimport java.util.HashMap; 38bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrickimport java.util.concurrent.atomic.AtomicInteger; 3946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 40438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick/** 4132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>StrictMode is a developer tool which detects things you might be 4232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * doing by accident and brings them to your attention so you can fix 4332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * them. 4415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 4515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>StrictMode is most commonly used to catch accidental disk or 4615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * network access on the application's main thread, where UI 4715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * operations are received and animations take place. Keeping disk 4815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * and network operations off the main thread makes for much smoother, 499fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * more responsive applications. By keeping your application's main thread 509fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * responsive, you also prevent 519fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * <a href="{@docRoot}guide/practices/design/responsiveness.html">ANR dialogs</a> 529fc2fc5757a3d28d098bd2b0ad0f869a3cf3fa14Brad Fitzpatrick * from being shown to users. 5315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 5415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">Note that even though an Android device's disk is 5515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * often on flash memory, many devices run a filesystem on top of that 5615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * memory with very limited concurrency. It's often the case that 5715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * almost all disk accesses are fast, but may in individual cases be 5815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * dramatically slower when certain I/O is happening in the background 5915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * from other processes. If possible, it's best to assume that such 6015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * things are not fast.</p> 6115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>Example code to enable from early in your 6315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application}, {@link android.app.Activity}, or 6415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * other application component's 6515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.app.Application#onCreate} method: 6615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 6715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <pre> 6815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * public void onCreate() { 6915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * if (DEVELOPER_MODE) { 7032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setThreadPolicy(new {@link ThreadPolicy.Builder StrictMode.ThreadPolicy.Builder}() 7132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskReads() 7232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectDiskWrites() 7332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectNetwork() // or .detectAll() for all detectable problems 7432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 7532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 7632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(new {@link VmPolicy.Builder StrictMode.VmPolicy.Builder}() 7762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * .detectLeakedSqlLiteObjects() 78fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * .detectLeakedClosableObjects() 7932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 8032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyDeath() 8132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build()); 8215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 8315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * super.onCreate(); 8415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * } 8515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * </pre> 8615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 8732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can decide what should happen when a violation is detected. 8832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * For example, using {@link ThreadPolicy.Builder#penaltyLog} you can 8932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * watch the output of <code>adb logcat</code> while you use your 9032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * application to see the violations as they happen. 9115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 9215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p>If you find violations that you feel are problematic, there are 9315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * a variety of tools to help solve them: threads, {@link android.os.Handler}, 9415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.AsyncTask}, {@link android.app.IntentService}, etc. 9515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * But don't feel compelled to fix everything that StrictMode finds. In particular, 9632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * many cases of disk access are often necessary during the normal activity lifecycle. Use 9732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode to find things you did by accident. Network requests on the UI thread 9815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * are almost always a problem, though. 9915ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 10015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * <p class="note">StrictMode is not a security mechanism and is not 10115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * guaranteed to find all disk or network accesses. While it does 10215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagate its state across process boundaries when doing 10315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * {@link android.os.Binder} calls, it's still ultimately a best 10415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * effort mechanism. Notably, disk or network access from JNI calls 10515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * won't necessarily trigger it. Future versions of Android may catch 10615ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * more (or fewer) operations, so you should never leave StrictMode 10715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * enabled in shipping applications on the Android Market. 108438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 109438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrickpublic final class StrictMode { 110438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static final String TAG = "StrictMode"; 11182829ef3b7c72bee36d8c17b36ac565f1856a310Brad Fitzpatrick private static final boolean LOG_V = Log.isLoggable(TAG, Log.VERBOSE); 112438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1131181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick private static final boolean IS_USER_BUILD = "user".equals(Build.TYPE); 1146804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick private static final boolean IS_ENG_BUILD = "eng".equals(Build.TYPE); 1151181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 116c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick /** 117c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick * The boolean system property to control screen flashes on violations. 118c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick * 119c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick * @hide 120c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick */ 121c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick public static final String VISUAL_PROPERTY = "persist.sys.strictmode.visual"; 122c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 12346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only log a duplicate stack trace to the logs every second. 12446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_LOG_INTERVAL_MS = 1000; 12546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 12646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Only show an annoying dialog at most every 30 seconds 12746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private static final long MIN_DIALOG_INTERVAL_MS = 30000; 12846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 129e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // How many Span tags (e.g. animations) to report. 130e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static final int MAX_SPAN_TAGS = 20; 131e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 132191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // How many offending stacks to keep track of (and time) per loop 133191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick // of the Looper. 134191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final int MAX_OFFENSES_PER_LOOP = 10; 135191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 13632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Thread-policy: 137438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 13815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 13932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 14015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 14132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_WRITE = 0x01; // for ThreadPolicy 14215ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 14315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 14432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 14515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 14632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_DISK_READ = 0x02; // for ThreadPolicy 14715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick 14815ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick /** 14932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 15015ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick */ 15132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_NETWORK = 0x04; // for ThreadPolicy 152438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 15332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Process-policy: 154438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 155438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 15632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Note, a "VM_" bit, not thread. 15732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 15832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 15932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final int DETECT_VM_CURSOR_LEAKS = 0x200; // for ProcessPolicy 16032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 16132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 162fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Note, a "VM_" bit, not thread. 163fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 164fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 165fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public static final int DETECT_VM_CLOSABLE_LEAKS = 0x400; // for ProcessPolicy 166fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 167fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 16832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 169438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 170438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_LOG = 0x10; // normal android.util.Log 171438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 17232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Used for both process and thread policy: 17332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 174438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 17532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 176438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 177438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DIALOG = 0x20; 178438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 179438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 180b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death on any detected violation. 181b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 18232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 183438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 184438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DEATH = 0x40; 185438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 186438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 187b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Death just for detected network usage. 188b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 189b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 190b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 191b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static final int PENALTY_DEATH_ON_NETWORK = 0x200; 192b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 193b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 1946804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * Flash the screen during violations. 1956804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * 1966804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * @hide 1976804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick */ 1986804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick public static final int PENALTY_FLASH = 0x800; 1996804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 2006804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick /** 20132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 202438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 203438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public static final int PENALTY_DROPBOX = 0x80; 204438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 205727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 206727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Non-public penalty mode which overrides all the other penalty 207727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * bits and signals that we're in a Binder call and we should 208727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * ignore the other penalty bits and instead serialize back all 209727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * our offending stack traces to the caller to ultimately handle 210727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * in the originating process. 211727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * 212703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * This must be kept in sync with the constant in libs/binder/Parcel.cpp 213703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * 214727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * @hide 215727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 216727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick public static final int PENALTY_GATHER = 0x100; 217727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 21832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 21971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick * Mask of all the penalty bits. 22071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick */ 22171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick private static final int PENALTY_MASK = 222b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick PENALTY_LOG | PENALTY_DIALOG | PENALTY_DEATH | PENALTY_DROPBOX | PENALTY_GATHER | 2236804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick PENALTY_DEATH_ON_NETWORK | PENALTY_FLASH; 22471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 22571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick /** 22632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The current VmPolicy in effect. 22732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 22832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static volatile int sVmPolicyMask = 0; 22932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 230bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick /** 231bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * The number of threads trying to do an async dropbox write. 232bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * Just to limit ourselves out of paranoia. 233bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick */ 234bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick private static final AtomicInteger sDropboxCallsInFlight = new AtomicInteger(0); 235bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 23632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private StrictMode() {} 23732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 23832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 23932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to a certain thread. 24032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 24132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setThreadPolicy}. The current policy 24232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can be retrieved with {@link #getThreadPolicy}. 24332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 24432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note that multiple penalties may be provided and they're run 24532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * in order from least to most severe (logging before process 24632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * death, for example). There's currently no mechanism to choose 24732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * different penalties for different detected actions. 24832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 24932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class ThreadPolicy { 25032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 25132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 25232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 25332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final ThreadPolicy LAX = new ThreadPolicy(0); 25432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 25532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 25632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 25732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private ThreadPolicy(int mask) { 25832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 25932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 26032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 26132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 26232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 26332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.ThreadPolicy; mask=" + mask + "]"; 26432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 26532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 26632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 26732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Creates ThreadPolicy instances. Methods whose names start 26832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 26932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 27032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 27132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 27232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 27332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 27432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 27532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 27632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 27732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 27832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() 27932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 28032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 28132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 28232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(policy); 28332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 28432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 28532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 28632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask = 0; 28732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 28832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 28932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Create a Builder that detects nothing and has no 29032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations. (but note that {@link #build} will default 29132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to enabling {@link #penaltyLog} if no other penalties 29232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * are specified) 29332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 29432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder() { 29532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = 0; 29632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 29732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 29832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 29932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Initialize a Builder from an existing ThreadPolicy. 30032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 30132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder(ThreadPolicy policy) { 30232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask = policy.mask; 30332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 30432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 30532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 30632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 30732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 30832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>As of the Gingerbread release this includes network and 30932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * disk operations but will likely expand in future releases. 31032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 31232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK); 31332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 31432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 31532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 31632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable the detection of everything. 31732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 31832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitAll() { 31932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_WRITE | DETECT_DISK_READ | DETECT_NETWORK); 32032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 32132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 32332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of network operations. 32432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 32532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectNetwork() { 32632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_NETWORK); 32732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 32832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 32932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 33032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of network operations. 33132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 33232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitNetwork() { 33332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_NETWORK); 33432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 33532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 33632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 33732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk reads. 33832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 33932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskReads() { 34032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_READ); 34132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 34232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 34332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 34432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk reads. 34532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 34632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskReads() { 34732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_READ); 34832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 34932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 35032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 35132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detection of disk writes. 35232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 35332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectDiskWrites() { 35432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_DISK_WRITE); 35532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 35632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 35732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 35832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Disable detection of disk writes. 35932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 36032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder permitDiskWrites() { 36132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return disable(DETECT_DISK_WRITE); 36232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 36332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 36432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 36532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Show an annoying dialog to the developer on detected 36632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violations, rate-limited to be only a little annoying. 36732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 36832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDialog() { 36932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DIALOG); 37032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 37132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 37232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 37332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crash the whole process on violation. This penalty runs at 37432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so you'll still get 37532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * see logging or other violations before the process dies. 376b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 377b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>Unlike {@link #penaltyDeathOnNetwork}, this applies 378b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * to disk reads, disk writes, and network usage if their 379b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * corresponding detect flags are set. 38032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 38132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 38232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 38332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 38432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 38532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 386b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Crash the whole process on any network usage. Unlike 387b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * {@link #penaltyDeath}, this penalty runs 388b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <em>before</em> anything else. You must still have 389b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * called {@link #detectNetwork} to enable this. 390b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 391b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * <p>In the Honeycomb or later SDKs, this is on by default. 392b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 393b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public Builder penaltyDeathOnNetwork() { 394b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick return enable(PENALTY_DEATH_ON_NETWORK); 395b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 396b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 397b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 3986804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick * Flash the screen during a violation. 3996804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick */ 4006804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick public Builder penaltyFlashScreen() { 4016804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick return enable(PENALTY_FLASH); 4026804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 4036804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 4046804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick /** 40532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 40632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 40732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 40832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 40932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 41032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 41132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 41232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 41332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 41432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 41532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 41632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 41732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 41832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 41932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 42032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 42132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 42232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 42332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 42432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 42532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 42632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder disable(int bit) { 42732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask &= ~bit; 42832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 42932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 43032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 43132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 43232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the ThreadPolicy instance. 43332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 43432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 43532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 43632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 43732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 43832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public ThreadPolicy build() { 43932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 44032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 44132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 44232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 44332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 44432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 44532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(mMask); 44732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 44932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 45032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 45132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 45232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link StrictMode} policy applied to all threads in the virtual machine's process. 45332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 45432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>The policy is enabled by {@link #setVmPolicy}. 45532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 45632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class VmPolicy { 45732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 45832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * The default, lax policy which doesn't catch anything. 45932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 46032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final VmPolicy LAX = new VmPolicy(0); 46132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 46232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int mask; 46332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 46432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private VmPolicy(int mask) { 46532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick this.mask = mask; 46632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 46732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 46832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick @Override 46932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public String toString() { 47032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return "[StrictMode.VmPolicy; mask=" + mask + "]"; 47132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 47232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 47332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 47432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Creates {@link VmPolicy} instances. Methods whose names start 47532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@code detect} specify what problems we should look 47632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * for. Methods whose names start with {@code penalty} specify what 47732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * we should do when we detect a problem. 47832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 47932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You can call as many {@code detect} and {@code penalty} 48032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * methods as you like. Currently order is insignificant: all 48132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * penalties apply to all detected problems. 48232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 48332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>For example, detect everything and log anything that's found: 48432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <pre> 48532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.VmPolicy policy = new StrictMode.VmPolicy.Builder() 48632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .detectAll() 48732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .penaltyLog() 48832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * .build(); 48932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * StrictMode.setVmPolicy(policy); 49032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * </pre> 49132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 49232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static final class Builder { 49332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private int mMask; 49432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 49532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 49632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect everything that's potentially suspect. 49732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 498fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>In the Honeycomb release this includes leaks of 499fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * SQLite cursors and other closable objects but will 500fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * likely expand in future releases. 50132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 50232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectAll() { 503fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return enable(DETECT_VM_CURSOR_LEAKS | DETECT_VM_CLOSABLE_LEAKS); 50432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 50532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 50632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 50732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Detect when an 50832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link android.database.sqlite.SQLiteCursor} or other 50932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * SQLite object is finalized without having been closed. 51032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 51132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>You always want to explicitly close your SQLite 51232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * cursors to avoid unnecessary database contention and 51332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * temporary memory leaks. 51432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 51532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder detectLeakedSqlLiteObjects() { 51632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(DETECT_VM_CURSOR_LEAKS); 51732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 51832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 51932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 520fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * Detect when an {@link java.io.Closeable} or other 521fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * object with a explict termination method is finalized 522fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * without having been closed. 523fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * 524fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * <p>You always want to explicitly close such objects to 525fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * avoid unnecessary resources leaks. 526fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 527fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public Builder detectLeakedClosableObjects() { 528fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return enable(DETECT_VM_CLOSABLE_LEAKS); 529fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 530fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 531fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 53232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Crashes the whole process on violation. This penalty runs at 53332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * the end of all enabled penalties so yo you'll still get 53432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * your logging or other violations before the process dies. 53532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 53632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDeath() { 53732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DEATH); 53832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 53932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 54032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 54132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Log detected violations to the system log. 54232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 54332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyLog() { 54432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_LOG); 54532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 54632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 54732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 54832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Enable detected violations log a stacktrace and timing data 54932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to the {@link android.os.DropBoxManager DropBox} on policy 55032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * violation. Intended mostly for platform integrators doing 55132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * beta user field data collection. 55232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 55332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public Builder penaltyDropBox() { 55432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return enable(PENALTY_DROPBOX); 55532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 55632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 55732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private Builder enable(int bit) { 55832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick mMask |= bit; 55932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return this; 56032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 56132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 56232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 56332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Construct the VmPolicy instance. 56432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 56532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Note: if no penalties are enabled before calling 56632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <code>build</code>, {@link #penaltyLog} is implicitly 56732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * set. 56832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 56932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public VmPolicy build() { 57032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // If there are detection bits set but no violation bits 57132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // set, enable simple logging. 57232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (mMask != 0 && 57332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick (mMask & (PENALTY_DEATH | PENALTY_LOG | 57432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick PENALTY_DROPBOX | PENALTY_DIALOG)) == 0) { 57532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick penaltyLog(); 57632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 57732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new VmPolicy(mMask); 57832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 57932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 58032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 581438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 582438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 5835b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Log of strict mode violation stack traces that have occurred 5845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * during a Binder call, to be serialized back later to the caller 5855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * via Parcel.writeNoException() (amusingly) where the caller can 5865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * choose how to react. 5875b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 588cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> gatheredViolations = 589cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 590cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 591703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // Starts null to avoid unnecessary allocations when 592703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // checking whether there are any violations or not in 593703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick // hasGatheredViolations() below. 594703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return null; 5955b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 5965b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick }; 5975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 5985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 59932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions on the current thread should 60032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * be detected, as well as the penalty if such actions occur. 60115ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * 60232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * <p>Internally this sets a thread-local variable which is 60315ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * propagated across cross-process IPC calls, meaning you can 60415ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * catch violations when a system service or another process 60515ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * accesses the disk or network on your behalf. 606438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 60732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 608438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 60932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setThreadPolicy(final ThreadPolicy policy) { 61032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(policy.mask); 61132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 61232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 61332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick private static void setThreadPolicyMask(final int policyMask) { 614727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // In addition to the Java-level thread-local in Dalvik's 615727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // BlockGuard, we also need to keep a native thread-local in 616727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Binder in order to propagate the value across Binder calls, 617727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // even across native-only processes. The two are kept in 618727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // sync via the callback to onStrictModePolicyChange, below. 619727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(policyMask); 620727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 621727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // And set the Android native version... 622727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick Binder.setThreadStrictModePolicy(policyMask); 623727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 624727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 625727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Sets the policy in Dalvik/libcore (BlockGuard) 626727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void setBlockGuardPolicy(final int policyMask) { 62746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick if (policyMask == 0) { 62846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick BlockGuard.setThreadPolicy(BlockGuard.LAX_POLICY); 62946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick return; 63046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 631438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 632438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick if (!(policy instanceof AndroidBlockGuardPolicy)) { 633438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick BlockGuard.setThreadPolicy(new AndroidBlockGuardPolicy(policyMask)); 634438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 635438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick AndroidBlockGuardPolicy androidPolicy = (AndroidBlockGuardPolicy) policy; 636438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick androidPolicy.setPolicyMask(policyMask); 637438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 638438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 639438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 6404b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom // Sets up CloseGuard in Dalvik/libcore 6414b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom private static void setCloseGuardEnabled(boolean enabled) { 6427c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick if (!(CloseGuard.getReporter() instanceof AndroidCloseGuardReporter)) { 6434b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom CloseGuard.setReporter(new AndroidCloseGuardReporter()); 6444b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 6454b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom CloseGuard.setEnabled(enabled); 6464b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 6474b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 6485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeNetworkViolation extends BlockGuard.BlockGuardPolicyException { 6495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeNetworkViolation(int policyMask) { 65032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_NETWORK); 6515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6535b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 6545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeDiskReadViolation extends BlockGuard.BlockGuardPolicyException { 6555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskReadViolation(int policyMask) { 65632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_DISK_READ); 6575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6595b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 6605b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class StrictModeDiskWriteViolation extends BlockGuard.BlockGuardPolicyException { 6615b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public StrictModeDiskWriteViolation(int policyMask) { 66232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick super(policyMask, DETECT_DISK_WRITE); 6635b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6645b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 6655b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 666438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick /** 66715ba4061116e088d62a7e05a0037f294f31dff06Brad Fitzpatrick * Returns the bitmask of the current thread's policy. 668438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick * 66932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the bitmask of all the DETECT_* and PENALTY_* bits currently enabled 67032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 67132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 672438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick */ 67332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static int getThreadPolicyMask() { 674438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return BlockGuard.getThreadPolicy().getPolicyMask(); 675438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 676438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 6775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 67832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Returns the current thread's policy. 67932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 68032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy getThreadPolicy() { 68132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(getThreadPolicyMask()); 68232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 68332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 68432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 68532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 68632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 68732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit both disk reads & writes, and sets the new policy 68832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 68932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 69097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 69132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to {@link #setThreadPolicy} to 69232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * restore the policy at the end of a block 69397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 69432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskWrites() { 69532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 69632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_WRITE | DETECT_DISK_READ); 69732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 69832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 69997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 70032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 70197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 70297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 70397461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 70432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * A convenience wrapper that takes the current 70532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * {@link ThreadPolicy} from {@link #getThreadPolicy}, modifies it 70632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * to permit disk reads, and sets the new policy 70732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * with {@link #setThreadPolicy}, returning the old policy so you 70832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * can restore it at the end of a block. 70997461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * 71032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @return the old policy, to be passed to setThreadPolicy to 71197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick * restore the policy. 71297461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick */ 71332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static ThreadPolicy allowThreadDiskReads() { 71432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int oldPolicyMask = getThreadPolicyMask(); 71532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int newPolicyMask = oldPolicyMask & ~(DETECT_DISK_READ); 71632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if (newPolicyMask != oldPolicyMask) { 71732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(newPolicyMask); 71897461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 71932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new ThreadPolicy(oldPolicyMask); 72097461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick } 72197461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick 722f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // We don't want to flash the screen red in the system server 723f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // process, nor do we want to modify all the call sites of 724f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // conditionallyEnableDebugLogging() in the system server, 725f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // so instead we use this to determine if we are the system server. 726f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick private static boolean amTheSystemServerProcess() { 727f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // Fast path. Most apps don't have the system server's UID. 728f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick if (Process.myUid() != Process.SYSTEM_UID) { 729f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick return false; 730f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 731f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick 732f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // The settings app, though, has the system server's UID so 733f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick // look up our stack to see if we came from the system server. 734f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick Throwable stack = new Throwable(); 735f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick stack.fillInStackTrace(); 736f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick for (StackTraceElement ste : stack.getStackTrace()) { 737f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick String clsName = ste.getClassName(); 738f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick if (clsName != null && clsName.startsWith("com.android.server.")) { 739f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick return true; 740f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 741f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 742f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick return false; 743f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick } 744f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick 74597461bd25c3821f3fb6af9705f0612259c6b4492Brad Fitzpatrick /** 74650d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * Enable DropBox logging for debug phone builds. 74750d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * 74850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick * @hide 74950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick */ 75050d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick public static boolean conditionallyEnableDebugLogging() { 751f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick boolean doFlashes = !amTheSystemServerProcess() && 752f54545927f365d6e55cbf66ff9f7ffe91aada774Brad Fitzpatrick SystemProperties.getBoolean(VISUAL_PROPERTY, IS_ENG_BUILD); 753c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 75450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // For debug builds, log event loop stalls to dropbox for analysis. 75550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick // Similar logic also appears in ActivityThread.java for system apps. 756c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick if (IS_USER_BUILD && !doFlashes) { 7577c2ae6570321575ad74a25bdc72bea1ec6558660Brad Fitzpatrick setCloseGuardEnabled(false); 75850d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return false; 75950d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 760c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 761c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick int threadPolicyMask = StrictMode.DETECT_DISK_WRITE | 762c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.DETECT_DISK_READ | 763c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.DETECT_NETWORK; 764c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 765c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick if (!IS_USER_BUILD) { 766c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick threadPolicyMask |= StrictMode.PENALTY_DROPBOX; 767c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } 768c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick if (doFlashes) { 769c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick threadPolicyMask |= StrictMode.PENALTY_FLASH; 770c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } 771c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 772c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.setThreadPolicyMask(threadPolicyMask); 773c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick 774c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick if (IS_USER_BUILD) { 775c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick setCloseGuardEnabled(false); 776c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } else { 777c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick sVmPolicyMask = StrictMode.DETECT_VM_CURSOR_LEAKS | 778c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.DETECT_VM_CLOSABLE_LEAKS | 779c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick StrictMode.PENALTY_DROPBOX; 780c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); 781c1a968a8ed45181312f7d4bcdbba0cc8ddc201baBrad Fitzpatrick } 78250d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick return true; 78350d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick } 78450d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick 78550d66f9fcdac84b2af65a82be56728f54b1a7ef0Brad Fitzpatrick /** 786b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * Used by the framework to make network usage on the main 787b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * thread a fatal error. 788b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * 789b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick * @hide 790b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick */ 791b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick public static void enableDeathOnNetwork() { 792b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int oldPolicy = getThreadPolicyMask(); 793b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick int newPolicy = oldPolicy | DETECT_NETWORK | PENALTY_DEATH_ON_NETWORK; 794b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick setThreadPolicyMask(newPolicy); 795b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 796b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick 797b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick /** 7985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Parses the BlockGuard policy mask out from the Exception's 7995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * getMessage() String value. Kinda gross, but least 8005b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * invasive. :/ 8015b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 8025b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Input is of form "policy=137 violation=64" 8035b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * 8045b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Returns 0 on failure, which is a valid policy, but not a 8055b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * valid policy during a violation (else there must've been 8065b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * some policy in effect to violate). 8075b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 8085b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parsePolicyFromMessage(String message) { 8095b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null || !message.startsWith("policy=")) { 8105b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 8115b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8125b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int spaceIndex = message.indexOf(' '); 8135b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (spaceIndex == -1) { 8145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 8155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String policyString = message.substring(7, spaceIndex); 8175b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 8185b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(policyString).intValue(); 8195b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 8205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 8215b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 8245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 8255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Like parsePolicyFromMessage(), but returns the violation. 8265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 8275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static int parseViolationFromMessage(String message) { 8285b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (message == null) { 8295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 8305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int violationIndex = message.indexOf("violation="); 8325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (violationIndex == -1) { 8335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 8345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String violationString = message.substring(violationIndex + 10); 8365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick try { 8375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return Integer.valueOf(violationString).intValue(); 8385b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } catch (NumberFormatException e) { 8395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return 0; 8405b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 843191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static final ThreadLocal<ArrayList<ViolationInfo>> violationsBeingTimed = 844191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick new ThreadLocal<ArrayList<ViolationInfo>>() { 845191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick @Override protected ArrayList<ViolationInfo> initialValue() { 846191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return new ArrayList<ViolationInfo>(); 847191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 848191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick }; 849191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 850bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // Note: only access this once verifying the thread has a Looper. 851bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick private static final ThreadLocal<Handler> threadHandler = new ThreadLocal<Handler>() { 852bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick @Override protected Handler initialValue() { 853bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick return new Handler(); 854bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } 855bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick }; 856bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick 857191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick private static boolean tooManyViolationsThisLoop() { 858191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return violationsBeingTimed.get().size() >= MAX_OFFENSES_PER_LOOP; 859191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 860191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick 861438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private static class AndroidBlockGuardPolicy implements BlockGuard.Policy { 862438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick private int mPolicyMask; 86346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 86446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Map from violation stacktrace hashcode -> uptimeMillis of 86546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // last violation. No locking needed, as this is only 86646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // accessed by the same thread. 86746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick private final HashMap<Integer, Long> mLastViolationTime = new HashMap<Integer, Long>(); 868438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 869438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public AndroidBlockGuardPolicy(final int policyMask) { 870438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 871438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 872438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 8735b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick @Override 8745b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick public String toString() { 8755b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return "AndroidBlockGuardPolicy; mPolicyMask=" + mPolicyMask; 8765b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 8775b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 878438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 879438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public int getPolicyMask() { 880438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return mPolicyMask; 881438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 882438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 883438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 884438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onWriteToDisk() { 88532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_WRITE) == 0) { 886438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 887438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 888191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 889191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 890191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 891cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskWriteViolation(mPolicyMask); 892cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 893cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 894438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 895438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 896438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 897438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onReadFromDisk() { 89832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_DISK_READ) == 0) { 899438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 900438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 901191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 902191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 903191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 904cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeDiskReadViolation(mPolicyMask); 905cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 906cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 907438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 908438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 909438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick // Part of BlockGuard.Policy interface: 910438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void onNetwork() { 91132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((mPolicyMask & DETECT_NETWORK) == 0) { 912438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick return; 913438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 914b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick if ((mPolicyMask & PENALTY_DEATH_ON_NETWORK) != 0) { 915b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick throw new NetworkOnMainThreadException(); 916b6e18412af35bf724298796eed65ef1fbbe1925eBrad Fitzpatrick } 917191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (tooManyViolationsThisLoop()) { 918191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick return; 919191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick } 920cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick BlockGuard.BlockGuardPolicyException e = new StrictModeNetworkViolation(mPolicyMask); 921cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick e.fillInStackTrace(); 922cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick startHandlingViolationException(e); 923438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 924438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 925438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick public void setPolicyMask(int policyMask) { 926438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick mPolicyMask = policyMask; 927438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 928438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 9295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Start handling a violation that just started and hasn't 9305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // actually run yet (e.g. no disk write or network operation 9315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // has yet occurred). This sees if we're in an event loop 9325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread and, if so, uses it to roughly measure how long the 9335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation took. 9345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick void startHandlingViolationException(BlockGuard.BlockGuardPolicyException e) { 935cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ViolationInfo info = new ViolationInfo(e, e.getPolicy()); 936cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.violationUptimeMillis = SystemClock.uptimeMillis(); 937cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolationWithTimingAttempt(info); 938cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 939438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 940cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Attempts to fill in the provided ViolationInfo's 941cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // durationMillis field if this thread has a Looper we can use 942cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // to measure with. We measure from the time of violation 943cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // until the time the looper is idle again (right before 944cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // the next epoll_wait) 945cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolationWithTimingAttempt(final ViolationInfo info) { 946438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick Looper looper = Looper.myLooper(); 947cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 948cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Without a Looper, we're unable to time how long the 949cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // violation takes place. This case should be rare, as 950cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // most users will care about timing violations that 951cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // happen on their main UI thread. Note that this case is 952cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // also hit when a violation takes place in a Binder 953cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // thread, in "gather" mode. In this case, the duration 954cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // of the violation is computed by the ultimate caller and 955cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // its Looper, if any. 956cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // TODO: if in gather mode, ignore Looper.myLooper() and always 957cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // go into this immediate mode? 958438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick if (looper == null) { 959cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis = -1; // unknown (redundant, already set) 960cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(info); 961cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 962438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 963438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 964cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick final ArrayList<ViolationInfo> records = violationsBeingTimed.get(); 965191cdf023c3c1ab441087a77f7881c7bb376613aBrad Fitzpatrick if (records.size() >= MAX_OFFENSES_PER_LOOP) { 966cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // Not worth measuring. Too many offenses in one loop. 967cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 968cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 969cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.add(info); 970cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (records.size() > 1) { 971cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // There's already been a violation this loop, so we've already 972cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // registered an idle handler to process the list of violations 973cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick // at the end of this Looper's loop. 974cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick return; 975cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 976cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 9776804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick final IWindowManager windowManager = (info.policy & PENALTY_FLASH) != 0 ? 978cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick sWindowManager.get() : null; 9796804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick if (windowManager != null) { 9806804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick try { 9816804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick windowManager.showStrictModeViolation(true); 9826804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } catch (RemoteException unused) { 9836804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 9846804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick } 9856804433b0af50f33a338307ae8ddb50bc49e886bBrad Fitzpatrick 986bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // We post a runnable to a Handler (== delay 0 ms) for 987bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // measuring the end time of a violation instead of using 988bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // an IdleHandler (as was previously used) because an 989bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // IdleHandler may not run for quite a long period of time 990bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // if an ongoing animation is happening and continually 991bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // posting ASAP (0 ms) animation steps. Animations are 992bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // throttled back to 60fps via SurfaceFlinger/View 993bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // invalidates, _not_ by posting frame updates every 16 994bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // milliseconds. 995bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick threadHandler.get().post(new Runnable() { 996bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick public void run() { 997cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick long loopFinishTime = SystemClock.uptimeMillis(); 998bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick 999bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // Note: we do this early, before handling the 1000bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // violation below, as handling the violation 1001bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // may include PENALTY_DEATH and we don't want 1002bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick // to keep the red border on. 1003bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick if (windowManager != null) { 1004bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick try { 1005bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick windowManager.showStrictModeViolation(false); 1006bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } catch (RemoteException unused) { 1007bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } 1008bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick } 1009bea168c09d173fb99cfc91c562c4a497a5e7d2d2Brad Fitzpatrick 1010cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (int n = 0; n < records.size(); ++n) { 1011cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo v = records.get(n); 1012cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.violationNumThisLoop = n + 1; 1013cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick v.durationMillis = 1014cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick (int) (loopFinishTime - v.violationUptimeMillis); 1015cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick handleViolation(v); 1016cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1017cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick records.clear(); 1018cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1019cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick }); 10205b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1021438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 10225b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Note: It's possible (even quite likely) that the 10235b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // thread-local policy mask has changed from the time the 10245b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // violation fired and now (after the violating code ran) due 10255b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // to people who push/pop temporary policy in regions of code, 10265b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // hence the policy being passed around. 1027cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick void handleViolation(final ViolationInfo info) { 1028cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info == null || info.crashInfo == null || info.crashInfo.stackTrace == null) { 1029cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.wtf(TAG, "unexpected null stacktrace"); 10305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 10315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1032438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1033cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (LOG_V) Log.d(TAG, "handleViolation; policy=" + info.policy); 103446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1035cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_GATHER) != 0) { 1036cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 1037703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 1038cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations = new ArrayList<ViolationInfo>(1); 1039703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(violations); 1040703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else if (violations.size() >= 5) { 10415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Too many. In a loop or something? Don't gather them all. 10425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 10435b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1044cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick for (ViolationInfo previous : violations) { 1045cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.crashInfo.stackTrace.equals(previous.crashInfo.stackTrace)) { 10465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Duplicate. Don't log. 10475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick return; 10485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 10495b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1050cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violations.add(info); 1051727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick return; 1052727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 1053727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 105446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // Not perfect, but fast and good enough for dup suppression. 1055f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick Integer crashFingerprint = info.hashCode(); 105646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long lastViolationTime = 0; 105746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick if (mLastViolationTime.containsKey(crashFingerprint)) { 105846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick lastViolationTime = mLastViolationTime.get(crashFingerprint); 105946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 106046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long now = SystemClock.uptimeMillis(); 106146d42387464a651268648659e91d022566d4844cBrad Fitzpatrick mLastViolationTime.put(crashFingerprint, now); 106246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick long timeSinceLastViolationMillis = lastViolationTime == 0 ? 106346d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Long.MAX_VALUE : (now - lastViolationTime); 106446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1065cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_LOG) != 0 && 106646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_LOG_INTERVAL_MS) { 1067cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (info.durationMillis != -1) { 10685b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation; ~duration=" + 1069cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.durationMillis + " ms: " + info.crashInfo.stackTrace); 1070438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } else { 1071cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick Log.d(TAG, "StrictMode policy violation: " + info.crashInfo.stackTrace); 1072438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1073438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1074438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 107571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // The violationMaskSubset, passed to ActivityManager, is a 107646d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // subset of the original StrictMode policy bitmask, with 107746d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // only the bit violated and penalty bits to be executed 107846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick // by the ActivityManagerService remaining set. 1079cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationMaskSubset = 0; 108046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1081cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DIALOG) != 0 && 108246d42387464a651268648659e91d022566d4844cBrad Fitzpatrick timeSinceLastViolationMillis > MIN_DIALOG_INTERVAL_MS) { 1083cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DIALOG; 108446d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 108546d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1086cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DROPBOX) != 0 && lastViolationTime == 0) { 1087cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= PENALTY_DROPBOX; 108846d42387464a651268648659e91d022566d4844cBrad Fitzpatrick } 108946d42387464a651268648659e91d022566d4844cBrad Fitzpatrick 1090cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationMaskSubset != 0) { 1091cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int violationBit = parseViolationFromMessage(info.crashInfo.exceptionMessage); 1092cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset |= violationBit; 109332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 109471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 109571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick final boolean justDropBox = (info.policy & PENALTY_MASK) == PENALTY_DROPBOX; 109671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick if (justDropBox) { 109771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // If all we're going to ask the activity manager 109871678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // to do is dropbox it (the common case during 109971678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // platform development), we can avoid doing this 110071678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // call synchronously which Binder data suggests 110171678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // isn't always super fast, despite the implementation 110271678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // in the ActivityManager trying to be mostly async. 1103bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick dropboxViolationAsync(violationMaskSubset, info); 110471678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick return; 110571678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick } 110671678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick 110771678ddcc45d9cd4557f3bed8bba5382bf36b68bBrad Fitzpatrick // Normal synchronous call to the ActivityManager. 1108438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick try { 1109727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 1110727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 1111727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 1112727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // We restore the current policy below, in the finally block. 111332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 1114727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 1115438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 1116438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick RuntimeInit.getApplicationObject(), 1117cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationMaskSubset, 1118cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info); 1119438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } catch (RemoteException e) { 112046d42387464a651268648659e91d022566d4844cBrad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 1121727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } finally { 1122727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick // Restore the policy. 112332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 1124438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1125438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1126438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick 1127cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if ((info.policy & PENALTY_DEATH) != 0) { 1128438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick System.err.println("StrictMode policy violation with POLICY_DEATH; shutting down."); 1129438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick Process.killProcess(Process.myPid()); 1130438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick System.exit(10); 1131438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1132438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1133438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick } 1134727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick 1135bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick /** 1136bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * In the common case, as set by conditionallyEnableDebugLogging, 1137bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * we're just dropboxing any violations but not showing a dialog, 1138bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * not loggging, and not killing the process. In these cases we 1139bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * don't need to do a synchronous call to the ActivityManager. 1140bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * This is used by both per-thread and vm-wide violations when 1141bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick * applicable. 1142bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick */ 1143bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick private static void dropboxViolationAsync( 1144bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick final int violationMaskSubset, final ViolationInfo info) { 1145bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int outstanding = sDropboxCallsInFlight.incrementAndGet(); 1146bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (outstanding > 20) { 1147bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // What's going on? Let's not make make the situation 1148bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // worse and just not log. 1149bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick sDropboxCallsInFlight.decrementAndGet(); 1150bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick return; 1151bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1152bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1153bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (LOG_V) Log.d(TAG, "Dropboxing async; in-flight=" + outstanding); 1154bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1155bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick new Thread("callActivityManagerForStrictModeDropbox") { 1156bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick public void run() { 1157bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND); 1158bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick try { 11591065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick IActivityManager am = ActivityManagerNative.getDefault(); 11601065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick if (am == null) { 11611065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick Log.d(TAG, "No activity manager; failed to Dropbox violation."); 11621065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick } else { 11631065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick am.handleApplicationStrictModeViolation( 11641065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick RuntimeInit.getApplicationObject(), 11651065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick violationMaskSubset, 11661065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick info); 11671065685400335ef8c1220f34b4e896e7da603789Brad Fitzpatrick } 1168bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } catch (RemoteException e) { 1169bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick Log.e(TAG, "RemoteException handling StrictMode violation", e); 1170bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1171bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int outstanding = sDropboxCallsInFlight.decrementAndGet(); 1172bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (LOG_V) Log.d(TAG, "Dropbox complete; in-flight=" + outstanding); 1173bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1174bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick }.start(); 1175bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 1176bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 11774b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom private static class AndroidCloseGuardReporter implements CloseGuard.Reporter { 11784b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom public void report (String message, Throwable allocationSite) { 11794b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom onVmPolicyViolation(message, allocationSite); 11804b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 11814b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 11824b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 1183727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick /** 11845b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 11855b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 11865b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static boolean hasGatheredViolations() { 1187703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick return gatheredViolations.get() != null; 1188703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1189703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick 1190703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /** 1191703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Called from Parcel.writeException(), so we drop this memory and 1192703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * don't incorrectly attribute it to the wrong caller on the next 1193703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick * Binder call on this thread. 1194703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick */ 1195703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick /* package */ static void clearGatheredViolations() { 1196703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 11975b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 11985b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 11995b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 120032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Sets the policy for what actions in the VM process (on any 120132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * thread) should be detected, as well as the penalty if such 120232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * actions occur. 120332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * 120432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @param policy the policy to put into place 120532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 120632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void setVmPolicy(final VmPolicy policy) { 120732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick sVmPolicyMask = policy.mask; 12084b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom setCloseGuardEnabled(vmClosableObjectLeaksEnabled()); 120932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 121032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 121132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 121232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * Gets the current VM policy. 121332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 121432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static VmPolicy getVmPolicy() { 121532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return new VmPolicy(sVmPolicyMask); 121632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 121732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 121832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 121962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * Enable the recommended StrictMode defaults, with violations just being logged. 122062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * 122162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick * <p>This catches disk and network access on the main thread, as 1222fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * well as leaked SQLite cursors and unclosed resources. This is 1223fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * simply a wrapper around {@link #setVmPolicy} and {@link 1224fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * #setThreadPolicy}. 122562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick */ 122662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick public static void enableDefaults() { 122762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 122862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .detectAll() 122962a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 123062a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 123162a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 123262a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .detectLeakedSqlLiteObjects() 1233fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom .detectLeakedClosableObjects() 123462a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .penaltyLog() 123562a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick .build()); 123662a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick } 123762a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick 123862a1eb58bfafe8744d7a65f651e11b88fdb0938dBrad Fitzpatrick /** 123932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 124032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 124132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static boolean vmSqliteObjectLeaksEnabled() { 124232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick return (sVmPolicyMask & DETECT_VM_CURSOR_LEAKS) != 0; 124332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 124432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 124532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 124632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick * @hide 124732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick */ 1248fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom public static boolean vmClosableObjectLeaksEnabled() { 1249fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom return (sVmPolicyMask & DETECT_VM_CLOSABLE_LEAKS) != 0; 1250fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom } 1251fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom 1252fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom /** 1253fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom * @hide 1254fd9ddd1a40efc801dc7512950cb9336967b6f775Brian Carlstrom */ 125532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick public static void onSqliteObjectLeaked(String message, Throwable originStack) { 12564b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom onVmPolicyViolation(message, originStack); 12574b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom } 12584b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom 12594b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom /** 12604b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom * @hide 12614b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom */ 12624b9b7c38e8f52259f9d2f960072d35e8a1ab2129Brian Carlstrom public static void onVmPolicyViolation(String message, Throwable originStack) { 126332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick if ((sVmPolicyMask & PENALTY_LOG) != 0) { 126432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Log.e(TAG, message, originStack); 126532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 126632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1267bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick boolean penaltyDropbox = (sVmPolicyMask & PENALTY_DROPBOX) != 0; 1268bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick boolean penaltyDeath = (sVmPolicyMask & PENALTY_DEATH) != 0; 1269bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1270bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick int violationMaskSubset = PENALTY_DROPBOX | DETECT_VM_CURSOR_LEAKS; 1271bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick ViolationInfo info = new ViolationInfo(originStack, sVmPolicyMask); 1272bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick 1273bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDropbox && !penaltyDeath) { 1274bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // Common case for userdebug/eng builds. If no death and 1275bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // just dropboxing, we can do the ActivityManager call 1276bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick // asynchronously. 1277bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick dropboxViolationAsync(violationMaskSubset, info); 1278bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick return; 1279bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick } 128032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1281bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDropbox) { 128232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // The violationMask, passed to ActivityManager, is a 128332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // subset of the original StrictMode policy bitmask, with 128432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // only the bit violated and penalty bits to be executed 128532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // by the ActivityManagerService remaining set. 128632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick final int savedPolicyMask = getThreadPolicyMask(); 128732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick try { 128832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // First, remove any policy before we call into the Activity Manager, 128932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // otherwise we'll infinite recurse as we try to log policy violations 129032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // to disk, thus violating policy, thus requiring logging, etc... 129132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // We restore the current policy below, in the finally block. 129232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(0); 129332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 129432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick ActivityManagerNative.getDefault().handleApplicationStrictModeViolation( 129532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick RuntimeInit.getApplicationObject(), 129632e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick violationMaskSubset, 129732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick info); 129832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } catch (RemoteException e) { 129932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Log.e(TAG, "RemoteException trying to handle StrictMode violation", e); 130032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } finally { 130132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick // Restore the policy. 130232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick setThreadPolicyMask(savedPolicyMask); 130332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 130432e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 130532e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 1306bee248769d51adb335b71b329b3d7813c5c71851Brad Fitzpatrick if (penaltyDeath) { 130732e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.err.println("StrictMode VmPolicy violation with POLICY_DEATH; shutting down."); 130832e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick Process.killProcess(Process.myPid()); 130932e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick System.exit(10); 131032e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 131132e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick } 131232e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick 131332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick /** 13145b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.writeNoException() 13155b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 13165b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void writeGatheredViolationsToParcel(Parcel p) { 1317cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ArrayList<ViolationInfo> violations = gatheredViolations.get(); 1318703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (violations == null) { 1319703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(0); 1320703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } else { 1321703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick p.writeInt(violations.size()); 1322703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick for (int i = 0; i < violations.size(); ++i) { 1323703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.get(i).writeToParcel(p, 0 /* unused flags? */); 1324703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick } 1325703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick if (LOG_V) Log.d(TAG, "wrote violations to response parcel; num=" + violations.size()); 1326703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick violations.clear(); // somewhat redundant, as we're about to null the threadlocal 13275b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 1328703e5d3c7fbeb8ca0978045db01d40318f838612Brad Fitzpatrick gatheredViolations.set(null); 13295b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 13305b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 13315b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick private static class LogStackTrace extends Exception {} 13325b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 13335b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 13345b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * Called from Parcel.readException() when the exception is EX_STRICT_MODE_VIOLATIONS, 13355b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick * we here read back all the encoded violations. 13365b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick */ 13375b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /* package */ static void readAndHandleBinderCallViolations(Parcel p) { 13385b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick // Our own stack trace to append 13395b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick StringWriter sw = new StringWriter(); 1340cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick new LogStackTrace().printStackTrace(new PrintWriter(sw)); 13415b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick String ourStack = sw.toString(); 13425b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 134332e60c7942eeba920ec5c27b372ec0899fd75a20Brad Fitzpatrick int policyMask = getThreadPolicyMask(); 1344cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick boolean currentlyGathering = (policyMask & PENALTY_GATHER) != 0; 13455b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 13465b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick int numViolations = p.readInt(); 13475b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick for (int i = 0; i < numViolations; ++i) { 13485b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (LOG_V) Log.d(TAG, "strict mode violation stacks read from binder call. i=" + i); 1349cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ViolationInfo info = new ViolationInfo(p, !currentlyGathering); 1350cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick info.crashInfo.stackTrace += "# via Binder call with stack:\n" + ourStack; 13515b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick BlockGuard.Policy policy = BlockGuard.getThreadPolicy(); 13525b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick if (policy instanceof AndroidBlockGuardPolicy) { 1353cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick ((AndroidBlockGuardPolicy) policy).handleViolationWithTimingAttempt(info); 13545b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 13555b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 13565b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick } 13575b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick 13585b747191ff8ad43a54d41faf50436271d1d7fcc8Brad Fitzpatrick /** 1359727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * Called from android_util_Binder.cpp's 1360727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * android_os_Parcel_enforceInterface when an incoming Binder call 1361727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * requires changing the StrictMode policy mask. The role of this 1362727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * function is to ask Binder for its current (native) thread-local 1363727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * policy value and synchronize it to libcore's (Java) 1364727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick * thread-local policy value. 1365727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick */ 1366727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick private static void onBinderStrictModePolicyChange(int newPolicy) { 1367727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick setBlockGuardPolicy(newPolicy); 1368727de40c6bc7c6521a0542ea9def5d5c7b1c5e06Brad Fitzpatrick } 1369cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1370cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1371e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * A tracked, critical time span. (e.g. during an animation.) 1372e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1373e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * The object itself is a linked list node, to avoid any allocations 1374e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * during rapid span entries and exits. 1375e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1376e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1377e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1378e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public static class Span { 1379e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private String mName; 1380e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private long mCreateMillis; 1381e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private Span mNext; 1382e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private Span mPrev; // not used when in freeList, only active 1383e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private final ThreadSpanState mContainerState; 1384e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1385e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span(ThreadSpanState threadState) { 1386e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mContainerState = threadState; 1387e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1388e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 13891181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // Empty constructor for the NO_OP_SPAN 13901181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick protected Span() { 13911181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick mContainerState = null; 13921181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 13931181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 1394e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1395e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * To be called when the critical span is complete (i.e. the 1396e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * animation is done animating). This can be called on any 1397e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * thread (even a different one from where the animation was 1398e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * taking place), but that's only a defensive implementation 1399e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * measure. It really makes no sense for you to call this on 1400e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * thread other than that where you created it. 1401e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1402e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1403e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1404e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public void finish() { 1405e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = mContainerState; 1406e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1407e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mName == null) { 1408e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Duplicate finish call. Ignore. 1409e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return; 1410e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1411e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1412e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Remove ourselves from the active list. 1413e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mPrev != null) { 1414e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mPrev.mNext = mNext; 1415e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1416e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (mNext != null) { 1417e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick mNext.mPrev = mPrev; 1418e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1419e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mActiveHead == this) { 1420e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveHead = mNext; 1421e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1422e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 14231cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick state.mActiveSize--; 14241cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick 14251cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick if (LOG_V) Log.d(TAG, "Span finished=" + mName + "; size=" + state.mActiveSize); 14261cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick 1427e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mCreateMillis = -1; 1428e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mName = null; 1429e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mPrev = null; 1430e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mNext = null; 1431e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1432e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Add ourselves to the freeList, if it's not already 1433e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // too big. 1434e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mFreeListSize < 5) { 1435e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.mNext = state.mFreeListHead; 1436e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListHead = this; 1437e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListSize++; 1438e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1439e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1440e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1441e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1442e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 14431181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // The no-op span that's used in user builds. 14441181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick private static final Span NO_OP_SPAN = new Span() { 14451181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick public void finish() { 14461181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick // Do nothing. 14471181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 14481181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick }; 14491181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick 1450e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1451e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Linked lists of active spans and a freelist. 1452e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1453e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Locking notes: there's one of these structures per thread and 1454e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * all members of this structure (as well as the Span nodes under 1455e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * it) are guarded by the ThreadSpanState object instance. While 1456e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * in theory there'd be no locking required because it's all local 1457e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * per-thread, the finish() method above is defensive against 1458e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * people calling it on a different thread from where they created 1459e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * the Span, hence the locking. 1460e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1461e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static class ThreadSpanState { 1462e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public Span mActiveHead; // doubly-linked list. 1463e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public int mActiveSize; 1464e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public Span mFreeListHead; // singly-linked list. only changes at head. 1465e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public int mFreeListSize; 1466e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1467e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1468e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick private static final ThreadLocal<ThreadSpanState> sThisThreadSpanState = 1469e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick new ThreadLocal<ThreadSpanState>() { 1470e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick @Override protected ThreadSpanState initialValue() { 1471e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return new ThreadSpanState(); 1472e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1473e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick }; 1474e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1475cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick private static Singleton<IWindowManager> sWindowManager = new Singleton<IWindowManager>() { 1476cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick protected IWindowManager create() { 1477cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick return IWindowManager.Stub.asInterface(ServiceManager.getService("window")); 1478cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick } 1479cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick }; 1480cdcb73ef781b8f7d37d9f758409a0c7671517b37Brad Fitzpatrick 1481e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1482e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * Enter a named critical span (e.g. an animation) 1483e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1484e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>The name is an arbitary label (or tag) that will be applied 1485e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * to any strictmode violation that happens while this span is 1486e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * active. You must call finish() on the span when done. 1487e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1488e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>This will never return null, but on devices without debugging 1489e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * enabled, this may return a dummy object on which the finish() 1490e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * method is a no-op. 1491e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1492e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * <p>TODO: add CloseGuard to this, verifying callers call finish. 1493e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * 1494e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * @hide 1495e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1496e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public static Span enterCriticalSpan(String name) { 14971181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick if (IS_USER_BUILD) { 14981181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick return NO_OP_SPAN; 14991181cbbfd7c913c51d9836272ad30cfe851c4699Brad Fitzpatrick } 1500e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (name == null || name.isEmpty()) { 1501e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick throw new IllegalArgumentException("name must be non-null and non-empty"); 1502e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1503e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = sThisThreadSpanState.get(); 1504e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span span = null; 1505e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1506e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (state.mFreeListHead != null) { 1507e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span = state.mFreeListHead; 1508e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListHead = span.mNext; 1509e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mFreeListSize--; 1510e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } else { 1511e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick // Shouldn't have to do this often. 1512e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span = new Span(state); 1513e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1514e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mName = name; 1515e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mCreateMillis = SystemClock.uptimeMillis(); 1516e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mNext = state.mActiveHead; 1517e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mPrev = null; 1518e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveHead = span; 1519e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick state.mActiveSize++; 1520e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (span.mNext != null) { 1521e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick span.mNext.mPrev = span; 1522e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 15231cc13b6d1cc7203ad126b0708f0bf697e111264fBrad Fitzpatrick if (LOG_V) Log.d(TAG, "Span enter=" + name + "; size=" + state.mActiveSize); 1524e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1525e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick return span; 1526e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1527e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1528e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1529e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1530cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Parcelable that gets sent in Binder call headers back to callers 1531cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * to report violations that happened during a cross-process call. 1532cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 1533cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @hide 1534cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1535cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public static class ViolationInfo { 1536cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1537cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Stack and other stuff info. 1538cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1539cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final ApplicationErrorReport.CrashInfo crashInfo; 1540cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1541cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1542cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The strict mode policy mask at the time of violation. 1543cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1544cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public final int policy; 1545cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1546cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1547cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The wall time duration of the violation, when known. -1 when 1548cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * not known. 1549cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1550cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int durationMillis = -1; 1551cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1552cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1553599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick * The number of animations currently running. 1554599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick */ 1555599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick public int numAnimationsRunning = 0; 1556599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick 1557599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick /** 1558e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * List of tags from active Span instances during this 1559e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick * violation, or null for none. 1560e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick */ 1561e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick public String[] tags; 1562e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick 1563e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick /** 1564cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Which violation number this was (1-based) since the last Looper loop, 1565cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * from the perspective of the root caller (if it crossed any processes 1566cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * via Binder calls). The value is 0 if the root caller wasn't on a Looper 1567cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * thread. 1568cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1569cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public int violationNumThisLoop; 1570cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1571cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1572cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * The time (in terms of SystemClock.uptimeMillis()) that the 1573cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * violation occurred. 1574cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1575cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public long violationUptimeMillis; 1576cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1577cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1578bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * The action of the Intent being broadcast to somebody's onReceive 1579bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick * on this thread right now, or null. 1580bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick */ 1581bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick public String broadcastIntentAction; 1582bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick 1583bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick /** 1584cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an uninitialized instance of ViolationInfo 1585cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1586cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo() { 1587cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = null; 1588cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = 0; 1589cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1590cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1591cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1592cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from an exception. 1593cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1594cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Throwable tr, int policy) { 1595cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(tr); 1596cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = SystemClock.uptimeMillis(); 1597cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this.policy = policy; 1598599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick this.numAnimationsRunning = ValueAnimator.getCurrentAnimationsCount(); 1599bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick Intent broadcastIntent = ActivityThread.getIntentBeingBroadcast(); 1600bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntent != null) { 1601bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = broadcastIntent.getAction(); 1602bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 1603e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick ThreadSpanState state = sThisThreadSpanState.get(); 1604e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick synchronized (state) { 1605e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int spanActiveCount = state.mActiveSize; 1606e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (spanActiveCount > MAX_SPAN_TAGS) { 1607e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick spanActiveCount = MAX_SPAN_TAGS; 1608e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1609e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (spanActiveCount != 0) { 1610e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.tags = new String[spanActiveCount]; 1611e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick Span iter = state.mActiveHead; 1612e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int index = 0; 1613e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick while (iter != null && index < spanActiveCount) { 1614e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick this.tags[index] = iter.mName; 1615e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick index++; 1616e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick iter = iter.mNext; 1617e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1618e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1619e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1620cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1621cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1622f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick @Override 1623f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick public int hashCode() { 1624f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick int result = 17; 1625f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + crashInfo.stackTrace.hashCode(); 1626f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (numAnimationsRunning != 0) { 1627f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result *= 37; 1628f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1629f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (broadcastIntentAction != null) { 1630f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + broadcastIntentAction.hashCode(); 1631f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1632f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick if (tags != null) { 1633f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick for (String tag : tags) { 1634f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick result = 37 * result + tag.hashCode(); 1635f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1636f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1637f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick return result; 1638f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick } 1639f3d86be6d7d2999cd6bae236817688490df7da71Brad Fitzpatrick 1640cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1641cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 1642cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1643cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in) { 1644cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick this(in, false); 1645cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1646cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1647cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1648cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Create an instance of ViolationInfo initialized from a Parcel. 1649cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * 1650cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * @param unsetGatheringBit if true, the caller is the root caller 1651cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * and the gathering penalty should be removed. 1652cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1653cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public ViolationInfo(Parcel in, boolean unsetGatheringBit) { 1654cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo = new ApplicationErrorReport.CrashInfo(in); 1655cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick int rawPolicy = in.readInt(); 1656cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (unsetGatheringBit) { 1657cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy & ~PENALTY_GATHER; 1658cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } else { 1659cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick policy = rawPolicy; 1660cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1661cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick durationMillis = in.readInt(); 1662cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationNumThisLoop = in.readInt(); 1663599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick numAnimationsRunning = in.readInt(); 1664cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick violationUptimeMillis = in.readLong(); 1665bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick broadcastIntentAction = in.readString(); 1666e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick tags = in.readStringArray(); 1667cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1668cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1669cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1670cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Save a ViolationInfo instance to a parcel. 1671cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1672cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void writeToParcel(Parcel dest, int flags) { 1673cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.writeToParcel(dest, flags); 1674cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(policy); 1675cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(durationMillis); 1676cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeInt(violationNumThisLoop); 1677599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick dest.writeInt(numAnimationsRunning); 1678cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick dest.writeLong(violationUptimeMillis); 1679bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick dest.writeString(broadcastIntentAction); 1680e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick dest.writeStringArray(tags); 1681cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1682cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1683cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1684cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick /** 1685cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick * Dump a ViolationInfo instance to a Printer. 1686cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick */ 1687cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick public void dump(Printer pw, String prefix) { 1688cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick crashInfo.dump(pw, prefix); 1689cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "policy: " + policy); 1690cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (durationMillis != -1) { 1691cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "durationMillis: " + durationMillis); 1692cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1693cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick if (violationNumThisLoop != 0) { 1694cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationNumThisLoop: " + violationNumThisLoop); 1695cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1696599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick if (numAnimationsRunning != 0) { 1697599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick pw.println(prefix + "numAnimationsRunning: " + numAnimationsRunning); 1698599ca29986235e07f532c7b112507f6c39b5dba9Brad Fitzpatrick } 1699cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick pw.println(prefix + "violationUptimeMillis: " + violationUptimeMillis); 1700bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick if (broadcastIntentAction != null) { 1701bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick pw.println(prefix + "broadcastIntentAction: " + broadcastIntentAction); 1702bfb191998eba2ebc710ff9eb59480b10909ba4c9Brad Fitzpatrick } 1703e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick if (tags != null) { 1704e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick int index = 0; 1705e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick for (String tag : tags) { 1706e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick pw.println(prefix + "tag[" + (index++) + "]: " + tag); 1707e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1708e7520d89fe2c5dc9dd833ecd9769c981df855b61Brad Fitzpatrick } 1709cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1710cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick 1711cb9ceb1029036363a81952d8ed5dfcbc83e6ff72Brad Fitzpatrick } 1712438d0595121a7a2cdf19741e76e3c0e21a5c173dBrad Fitzpatrick} 1713