Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app; |
| 18 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 19 | import android.content.ComponentName; |
| 20 | import android.content.Context; |
| 21 | import android.content.Intent; |
| 22 | import android.content.pm.ApplicationInfo; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ResolveInfo; |
Jeff Sharkey | f5299f1 | 2017-03-17 10:25:07 -0600 | [diff] [blame] | 25 | import android.os.Binder; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 28 | import android.os.SystemProperties; |
| 29 | import android.provider.Settings; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 30 | import android.util.Printer; |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 31 | import android.util.Slog; |
Garfield Tan | 2746ab5 | 2018-07-25 12:33:01 -0700 | [diff] [blame] | 32 | |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 33 | import com.android.internal.util.FastPrintWriter; |
Brad Fitzpatrick | 5b74719 | 2010-07-12 11:05:38 -0700 | [diff] [blame] | 34 | |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 35 | import java.io.PrintWriter; |
| 36 | import java.io.StringWriter; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * Describes an application error. |
| 40 | * |
| 41 | * A report has a type, which is one of |
| 42 | * <ul> |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 43 | * <li> {@link #TYPE_NONE} uninitialized instance of {@link ApplicationErrorReport}. |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 44 | * <li> {@link #TYPE_CRASH} application crash. Information about the crash |
| 45 | * is stored in {@link #crashInfo}. |
| 46 | * <li> {@link #TYPE_ANR} application not responding. Information about the |
| 47 | * ANR is stored in {@link #anrInfo}. |
Dianne Hackborn | 271c2fe | 2011-08-09 19:35:13 -0700 | [diff] [blame] | 48 | * <li> {@link #TYPE_BATTERY} user reported application is using too much |
| 49 | * battery. Information about the battery use is stored in {@link #batteryInfo}. |
| 50 | * <li> {@link #TYPE_RUNNING_SERVICE} user reported application is leaving an |
| 51 | * unneeded serive running. Information about the battery use is stored in |
| 52 | * {@link #runningServiceInfo}. |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 53 | * </ul> |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 54 | */ |
| 55 | |
| 56 | public class ApplicationErrorReport implements Parcelable { |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 57 | // System property defining error report receiver for system apps |
| 58 | static final String SYSTEM_APPS_ERROR_RECEIVER_PROPERTY = "ro.error.receiver.system.apps"; |
| 59 | |
| 60 | // System property defining default error report receiver |
| 61 | static final String DEFAULT_ERROR_RECEIVER_PROPERTY = "ro.error.receiver.default"; |
| 62 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 63 | /** |
| 64 | * Uninitialized error report. |
| 65 | */ |
| 66 | public static final int TYPE_NONE = 0; |
| 67 | |
| 68 | /** |
| 69 | * An error report about an application crash. |
| 70 | */ |
| 71 | public static final int TYPE_CRASH = 1; |
| 72 | |
| 73 | /** |
| 74 | * An error report about an application that's not responding. |
| 75 | */ |
| 76 | public static final int TYPE_ANR = 2; |
| 77 | |
| 78 | /** |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 79 | * An error report about an application that's consuming too much battery. |
| 80 | */ |
| 81 | public static final int TYPE_BATTERY = 3; |
| 82 | |
| 83 | /** |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 84 | * A report from a user to a developer about a running service that the |
| 85 | * user doesn't think should be running. |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 86 | */ |
| 87 | public static final int TYPE_RUNNING_SERVICE = 5; |
| 88 | |
| 89 | /** |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 90 | * Type of this report. Can be one of {@link #TYPE_NONE}, |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 91 | * {@link #TYPE_CRASH}, {@link #TYPE_ANR}, {@link #TYPE_BATTERY}, |
| 92 | * or {@link #TYPE_RUNNING_SERVICE}. |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 93 | */ |
| 94 | public int type; |
| 95 | |
| 96 | /** |
| 97 | * Package name of the application. |
| 98 | */ |
| 99 | public String packageName; |
| 100 | |
| 101 | /** |
| 102 | * Package name of the application which installed the application this |
| 103 | * report pertains to. |
Dirk Dougherty | 4d7bc655 | 2012-01-27 17:56:49 -0800 | [diff] [blame] | 104 | * This identifies which market the application came from. |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 105 | */ |
| 106 | public String installerPackageName; |
| 107 | |
| 108 | /** |
| 109 | * Process name of the application. |
| 110 | */ |
| 111 | public String processName; |
| 112 | |
| 113 | /** |
| 114 | * Time at which the error occurred. |
| 115 | */ |
| 116 | public long time; |
| 117 | |
| 118 | /** |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 119 | * Set if the app is on the system image. |
| 120 | */ |
| 121 | public boolean systemApp; |
| 122 | |
| 123 | /** |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 124 | * If this report is of type {@link #TYPE_CRASH}, contains an instance |
| 125 | * of CrashInfo describing the crash; otherwise null. |
| 126 | */ |
| 127 | public CrashInfo crashInfo; |
| 128 | |
| 129 | /** |
| 130 | * If this report is of type {@link #TYPE_ANR}, contains an instance |
| 131 | * of AnrInfo describing the ANR; otherwise null. |
| 132 | */ |
| 133 | public AnrInfo anrInfo; |
| 134 | |
| 135 | /** |
Jacek Surazski | 151de3d | 2010-02-26 22:52:44 +0100 | [diff] [blame] | 136 | * If this report is of type {@link #TYPE_BATTERY}, contains an instance |
| 137 | * of BatteryInfo; otherwise null. |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 138 | */ |
Jacek Surazski | 151de3d | 2010-02-26 22:52:44 +0100 | [diff] [blame] | 139 | public BatteryInfo batteryInfo; |
Brad Fitzpatrick | cb9ceb1 | 2010-07-29 14:29:02 -0700 | [diff] [blame] | 140 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 141 | /** |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 142 | * If this report is of type {@link #TYPE_RUNNING_SERVICE}, contains an instance |
| 143 | * of RunningServiceInfo; otherwise null. |
| 144 | */ |
| 145 | public RunningServiceInfo runningServiceInfo; |
| 146 | |
| 147 | /** |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 148 | * Create an uninitialized instance of {@link ApplicationErrorReport}. |
| 149 | */ |
| 150 | public ApplicationErrorReport() { |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Create an instance of {@link ApplicationErrorReport} initialized from |
| 155 | * a parcel. |
| 156 | */ |
| 157 | ApplicationErrorReport(Parcel in) { |
Jacek Surazski | 28b0e5d | 2009-05-25 17:56:41 +0200 | [diff] [blame] | 158 | readFromParcel(in); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 159 | } |
| 160 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 161 | public static ComponentName getErrorReportReceiver(Context context, |
| 162 | String packageName, int appFlags) { |
| 163 | // check if error reporting is enabled in secure settings |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 164 | int enabled = Settings.Global.getInt(context.getContentResolver(), |
| 165 | Settings.Global.SEND_ACTION_APP_ERROR, 0); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 166 | if (enabled == 0) { |
| 167 | return null; |
| 168 | } |
| 169 | |
| 170 | PackageManager pm = context.getPackageManager(); |
| 171 | |
| 172 | // look for receiver in the installer package |
louis_chang | bd4a9a0 | 2014-07-31 10:47:45 +0800 | [diff] [blame] | 173 | String candidate = null; |
| 174 | ComponentName result = null; |
| 175 | |
| 176 | try { |
| 177 | candidate = pm.getInstallerPackageName(packageName); |
| 178 | } catch (IllegalArgumentException e) { |
| 179 | // the package could already removed |
| 180 | } |
| 181 | |
| 182 | if (candidate != null) { |
| 183 | result = getErrorReportReceiver(pm, packageName, candidate); |
| 184 | if (result != null) { |
| 185 | return result; |
| 186 | } |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | // if the error app is on the system image, look for system apps |
| 190 | // error receiver |
| 191 | if ((appFlags&ApplicationInfo.FLAG_SYSTEM) != 0) { |
| 192 | candidate = SystemProperties.get(SYSTEM_APPS_ERROR_RECEIVER_PROPERTY); |
| 193 | result = getErrorReportReceiver(pm, packageName, candidate); |
| 194 | if (result != null) { |
| 195 | return result; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // if there is a default receiver, try that |
| 200 | candidate = SystemProperties.get(DEFAULT_ERROR_RECEIVER_PROPERTY); |
| 201 | return getErrorReportReceiver(pm, packageName, candidate); |
| 202 | } |
Jacek Surazski | 87d0b2f | 2010-08-02 13:03:35 +0200 | [diff] [blame] | 203 | |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 204 | /** |
| 205 | * Return activity in receiverPackage that handles ACTION_APP_ERROR. |
| 206 | * |
Madan Ankapura | ae96f63 | 2010-06-18 18:16:58 -0700 | [diff] [blame] | 207 | * @param pm PackageManager instance |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 208 | * @param errorPackage package which caused the error |
| 209 | * @param receiverPackage candidate package to receive the error |
| 210 | * @return activity component within receiverPackage which handles |
| 211 | * ACTION_APP_ERROR, or null if not found |
| 212 | */ |
| 213 | static ComponentName getErrorReportReceiver(PackageManager pm, String errorPackage, |
| 214 | String receiverPackage) { |
| 215 | if (receiverPackage == null || receiverPackage.length() == 0) { |
| 216 | return null; |
| 217 | } |
| 218 | |
| 219 | // break the loop if it's the error report receiver package that crashed |
| 220 | if (receiverPackage.equals(errorPackage)) { |
| 221 | return null; |
| 222 | } |
| 223 | |
| 224 | Intent intent = new Intent(Intent.ACTION_APP_ERROR); |
| 225 | intent.setPackage(receiverPackage); |
| 226 | ResolveInfo info = pm.resolveActivity(intent, 0); |
| 227 | if (info == null || info.activityInfo == null) { |
| 228 | return null; |
| 229 | } |
| 230 | return new ComponentName(receiverPackage, info.activityInfo.name); |
| 231 | } |
| 232 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 233 | public void writeToParcel(Parcel dest, int flags) { |
| 234 | dest.writeInt(type); |
| 235 | dest.writeString(packageName); |
| 236 | dest.writeString(installerPackageName); |
| 237 | dest.writeString(processName); |
| 238 | dest.writeLong(time); |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 239 | dest.writeInt(systemApp ? 1 : 0); |
louis_chang | 3d86b88 | 2015-04-08 18:04:11 +0800 | [diff] [blame] | 240 | dest.writeInt(crashInfo != null ? 1 : 0); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 241 | |
| 242 | switch (type) { |
| 243 | case TYPE_CRASH: |
louis_chang | 3d86b88 | 2015-04-08 18:04:11 +0800 | [diff] [blame] | 244 | if (crashInfo != null) { |
| 245 | crashInfo.writeToParcel(dest, flags); |
| 246 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 247 | break; |
| 248 | case TYPE_ANR: |
| 249 | anrInfo.writeToParcel(dest, flags); |
| 250 | break; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 251 | case TYPE_BATTERY: |
Jacek Surazski | 151de3d | 2010-02-26 22:52:44 +0100 | [diff] [blame] | 252 | batteryInfo.writeToParcel(dest, flags); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 253 | break; |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 254 | case TYPE_RUNNING_SERVICE: |
| 255 | runningServiceInfo.writeToParcel(dest, flags); |
| 256 | break; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
Jacek Surazski | fd0bdcc | 2009-05-27 14:45:48 +0200 | [diff] [blame] | 260 | public void readFromParcel(Parcel in) { |
Jacek Surazski | 28b0e5d | 2009-05-25 17:56:41 +0200 | [diff] [blame] | 261 | type = in.readInt(); |
| 262 | packageName = in.readString(); |
| 263 | installerPackageName = in.readString(); |
| 264 | processName = in.readString(); |
| 265 | time = in.readLong(); |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 266 | systemApp = in.readInt() == 1; |
louis_chang | 3d86b88 | 2015-04-08 18:04:11 +0800 | [diff] [blame] | 267 | boolean hasCrashInfo = in.readInt() == 1; |
Jacek Surazski | 28b0e5d | 2009-05-25 17:56:41 +0200 | [diff] [blame] | 268 | |
| 269 | switch (type) { |
| 270 | case TYPE_CRASH: |
louis_chang | 3d86b88 | 2015-04-08 18:04:11 +0800 | [diff] [blame] | 271 | crashInfo = hasCrashInfo ? new CrashInfo(in) : null; |
Jacek Surazski | 28b0e5d | 2009-05-25 17:56:41 +0200 | [diff] [blame] | 272 | anrInfo = null; |
Jacek Surazski | 151de3d | 2010-02-26 22:52:44 +0100 | [diff] [blame] | 273 | batteryInfo = null; |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 274 | runningServiceInfo = null; |
Jacek Surazski | 28b0e5d | 2009-05-25 17:56:41 +0200 | [diff] [blame] | 275 | break; |
| 276 | case TYPE_ANR: |
| 277 | anrInfo = new AnrInfo(in); |
| 278 | crashInfo = null; |
Jacek Surazski | 151de3d | 2010-02-26 22:52:44 +0100 | [diff] [blame] | 279 | batteryInfo = null; |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 280 | runningServiceInfo = null; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 281 | break; |
| 282 | case TYPE_BATTERY: |
Jacek Surazski | 151de3d | 2010-02-26 22:52:44 +0100 | [diff] [blame] | 283 | batteryInfo = new BatteryInfo(in); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 284 | anrInfo = null; |
| 285 | crashInfo = null; |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 286 | runningServiceInfo = null; |
| 287 | break; |
| 288 | case TYPE_RUNNING_SERVICE: |
| 289 | batteryInfo = null; |
| 290 | anrInfo = null; |
| 291 | crashInfo = null; |
| 292 | runningServiceInfo = new RunningServiceInfo(in); |
Jacek Surazski | 28b0e5d | 2009-05-25 17:56:41 +0200 | [diff] [blame] | 293 | break; |
| 294 | } |
| 295 | } |
| 296 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 297 | /** |
| 298 | * Describes an application crash. |
| 299 | */ |
| 300 | public static class CrashInfo { |
| 301 | /** |
| 302 | * Class name of the exception that caused the crash. |
| 303 | */ |
| 304 | public String exceptionClassName; |
| 305 | |
| 306 | /** |
Jacek Surazski | f829a78 | 2009-06-11 22:47:02 +0200 | [diff] [blame] | 307 | * Message stored in the exception. |
| 308 | */ |
| 309 | public String exceptionMessage; |
| 310 | |
| 311 | /** |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 312 | * File which the exception was thrown from. |
| 313 | */ |
| 314 | public String throwFileName; |
| 315 | |
| 316 | /** |
| 317 | * Class which the exception was thrown from. |
| 318 | */ |
| 319 | public String throwClassName; |
| 320 | |
| 321 | /** |
| 322 | * Method which the exception was thrown from. |
| 323 | */ |
| 324 | public String throwMethodName; |
| 325 | |
| 326 | /** |
Jacek Surazski | 5a12373 | 2009-06-23 14:57:08 +0200 | [diff] [blame] | 327 | * Line number the exception was thrown from. |
| 328 | */ |
| 329 | public int throwLineNumber; |
| 330 | |
| 331 | /** |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 332 | * Stack trace. |
| 333 | */ |
| 334 | public String stackTrace; |
| 335 | |
| 336 | /** |
Garfield Tan | 2746ab5 | 2018-07-25 12:33:01 -0700 | [diff] [blame] | 337 | * Crash tag for some context. |
| 338 | * @hide |
| 339 | */ |
| 340 | public String crashTag; |
| 341 | |
| 342 | /** |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 343 | * Create an uninitialized instance of CrashInfo. |
| 344 | */ |
| 345 | public CrashInfo() { |
| 346 | } |
| 347 | |
| 348 | /** |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 349 | * Create an instance of CrashInfo initialized from an exception. |
| 350 | */ |
| 351 | public CrashInfo(Throwable tr) { |
| 352 | StringWriter sw = new StringWriter(); |
Dianne Hackborn | 8c84109 | 2013-06-24 13:46:13 -0700 | [diff] [blame] | 353 | PrintWriter pw = new FastPrintWriter(sw, false, 256); |
| 354 | tr.printStackTrace(pw); |
| 355 | pw.flush(); |
Adrian Roos | 732ae95 | 2016-07-01 13:11:18 -0700 | [diff] [blame] | 356 | stackTrace = sanitizeString(sw.toString()); |
Dan Egnor | 60d8762 | 2009-12-16 16:32:58 -0800 | [diff] [blame] | 357 | exceptionMessage = tr.getMessage(); |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 358 | |
| 359 | // Populate fields with the "root cause" exception |
Dianne Hackborn | 8e8d65f | 2011-08-11 19:36:18 -0700 | [diff] [blame] | 360 | Throwable rootTr = tr; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 361 | while (tr.getCause() != null) { |
| 362 | tr = tr.getCause(); |
Dianne Hackborn | 8e8d65f | 2011-08-11 19:36:18 -0700 | [diff] [blame] | 363 | if (tr.getStackTrace() != null && tr.getStackTrace().length > 0) { |
| 364 | rootTr = tr; |
| 365 | } |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 366 | String msg = tr.getMessage(); |
| 367 | if (msg != null && msg.length() > 0) { |
| 368 | exceptionMessage = msg; |
| 369 | } |
| 370 | } |
| 371 | |
Dianne Hackborn | 8e8d65f | 2011-08-11 19:36:18 -0700 | [diff] [blame] | 372 | exceptionClassName = rootTr.getClass().getName(); |
| 373 | if (rootTr.getStackTrace().length > 0) { |
| 374 | StackTraceElement trace = rootTr.getStackTrace()[0]; |
| 375 | throwFileName = trace.getFileName(); |
| 376 | throwClassName = trace.getClassName(); |
| 377 | throwMethodName = trace.getMethodName(); |
| 378 | throwLineNumber = trace.getLineNumber(); |
| 379 | } else { |
| 380 | throwFileName = "unknown"; |
| 381 | throwClassName = "unknown"; |
| 382 | throwMethodName = "unknown"; |
| 383 | throwLineNumber = 0; |
| 384 | } |
Adrian Roos | 732ae95 | 2016-07-01 13:11:18 -0700 | [diff] [blame] | 385 | |
| 386 | exceptionMessage = sanitizeString(exceptionMessage); |
| 387 | } |
| 388 | |
Jeff Sharkey | 58f27b5 | 2016-12-06 16:47:00 -0700 | [diff] [blame] | 389 | /** {@hide} */ |
| 390 | public void appendStackTrace(String tr) { |
| 391 | stackTrace = sanitizeString(stackTrace + tr); |
| 392 | } |
| 393 | |
Adrian Roos | 732ae95 | 2016-07-01 13:11:18 -0700 | [diff] [blame] | 394 | /** |
| 395 | * Ensure that the string is of reasonable size, truncating from the middle if needed. |
| 396 | */ |
| 397 | private String sanitizeString(String s) { |
| 398 | int prefixLength = 10 * 1024; |
| 399 | int suffixLength = 10 * 1024; |
| 400 | int acceptableLength = prefixLength + suffixLength; |
| 401 | |
| 402 | if (s != null && s.length() > acceptableLength) { |
| 403 | String replacement = |
| 404 | "\n[TRUNCATED " + (s.length() - acceptableLength) + " CHARS]\n"; |
| 405 | |
| 406 | StringBuilder sb = new StringBuilder(acceptableLength + replacement.length()); |
| 407 | sb.append(s.substring(0, prefixLength)); |
| 408 | sb.append(replacement); |
| 409 | sb.append(s.substring(s.length() - suffixLength)); |
| 410 | return sb.toString(); |
| 411 | } |
| 412 | return s; |
Dan Egnor | b7f0367 | 2009-12-09 16:22:32 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /** |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 416 | * Create an instance of CrashInfo initialized from a Parcel. |
| 417 | */ |
| 418 | public CrashInfo(Parcel in) { |
| 419 | exceptionClassName = in.readString(); |
Jacek Surazski | f829a78 | 2009-06-11 22:47:02 +0200 | [diff] [blame] | 420 | exceptionMessage = in.readString(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 421 | throwFileName = in.readString(); |
| 422 | throwClassName = in.readString(); |
| 423 | throwMethodName = in.readString(); |
Jacek Surazski | 5a12373 | 2009-06-23 14:57:08 +0200 | [diff] [blame] | 424 | throwLineNumber = in.readInt(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 425 | stackTrace = in.readString(); |
Garfield Tan | 2746ab5 | 2018-07-25 12:33:01 -0700 | [diff] [blame] | 426 | crashTag = in.readString(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Save a CrashInfo instance to a parcel. |
| 431 | */ |
| 432 | public void writeToParcel(Parcel dest, int flags) { |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 433 | int start = dest.dataPosition(); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 434 | dest.writeString(exceptionClassName); |
Jacek Surazski | f829a78 | 2009-06-11 22:47:02 +0200 | [diff] [blame] | 435 | dest.writeString(exceptionMessage); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 436 | dest.writeString(throwFileName); |
| 437 | dest.writeString(throwClassName); |
| 438 | dest.writeString(throwMethodName); |
Jacek Surazski | 5a12373 | 2009-06-23 14:57:08 +0200 | [diff] [blame] | 439 | dest.writeInt(throwLineNumber); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 440 | dest.writeString(stackTrace); |
Garfield Tan | 2746ab5 | 2018-07-25 12:33:01 -0700 | [diff] [blame] | 441 | dest.writeString(crashTag); |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 442 | int total = dest.dataPosition()-start; |
Jeff Sharkey | f5299f1 | 2017-03-17 10:25:07 -0600 | [diff] [blame] | 443 | if (Binder.CHECK_PARCEL_SIZE && total > 20*1024) { |
Dianne Hackborn | 73d6a82 | 2014-09-29 10:52:47 -0700 | [diff] [blame] | 444 | Slog.d("Error", "ERR: exClass=" + exceptionClassName); |
| 445 | Slog.d("Error", "ERR: exMsg=" + exceptionMessage); |
| 446 | Slog.d("Error", "ERR: file=" + throwFileName); |
| 447 | Slog.d("Error", "ERR: class=" + throwClassName); |
| 448 | Slog.d("Error", "ERR: method=" + throwMethodName + " line=" + throwLineNumber); |
| 449 | Slog.d("Error", "ERR: stack=" + stackTrace); |
| 450 | Slog.d("Error", "ERR: TOTAL BYTES WRITTEN: " + (dest.dataPosition()-start)); |
| 451 | } |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Dump a CrashInfo instance to a Printer. |
| 456 | */ |
| 457 | public void dump(Printer pw, String prefix) { |
| 458 | pw.println(prefix + "exceptionClassName: " + exceptionClassName); |
Jacek Surazski | f829a78 | 2009-06-11 22:47:02 +0200 | [diff] [blame] | 459 | pw.println(prefix + "exceptionMessage: " + exceptionMessage); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 460 | pw.println(prefix + "throwFileName: " + throwFileName); |
| 461 | pw.println(prefix + "throwClassName: " + throwClassName); |
| 462 | pw.println(prefix + "throwMethodName: " + throwMethodName); |
Jacek Surazski | 5a12373 | 2009-06-23 14:57:08 +0200 | [diff] [blame] | 463 | pw.println(prefix + "throwLineNumber: " + throwLineNumber); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 464 | pw.println(prefix + "stackTrace: " + stackTrace); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /** |
Sudheer Shanka | fc46e9b | 2016-10-21 17:55:27 -0700 | [diff] [blame] | 469 | * Parcelable version of {@link CrashInfo} |
| 470 | * |
| 471 | * @hide |
| 472 | */ |
| 473 | public static class ParcelableCrashInfo extends CrashInfo implements Parcelable { |
| 474 | /** |
| 475 | * Create an uninitialized instance of CrashInfo. |
| 476 | */ |
| 477 | public ParcelableCrashInfo() { |
| 478 | } |
| 479 | |
| 480 | /** |
| 481 | * Create an instance of CrashInfo initialized from an exception. |
| 482 | */ |
| 483 | public ParcelableCrashInfo(Throwable tr) { |
| 484 | super(tr); |
| 485 | } |
| 486 | |
| 487 | public ParcelableCrashInfo(Parcel in) { |
| 488 | super(in); |
| 489 | } |
| 490 | |
| 491 | public int describeContents() { |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | public static final Parcelable.Creator<ParcelableCrashInfo> CREATOR = |
| 496 | new Parcelable.Creator<ParcelableCrashInfo>() { |
| 497 | @Override |
| 498 | public ParcelableCrashInfo createFromParcel(Parcel in) { |
| 499 | return new ParcelableCrashInfo(in); |
| 500 | } |
| 501 | |
| 502 | @Override |
| 503 | public ParcelableCrashInfo[] newArray(int size) { |
| 504 | return new ParcelableCrashInfo[size]; |
| 505 | } |
| 506 | }; |
| 507 | } |
| 508 | |
| 509 | /** |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 510 | * Describes an application not responding error. |
| 511 | */ |
| 512 | public static class AnrInfo { |
| 513 | /** |
| 514 | * Activity name. |
| 515 | */ |
| 516 | public String activity; |
| 517 | |
| 518 | /** |
| 519 | * Description of the operation that timed out. |
| 520 | */ |
| 521 | public String cause; |
| 522 | |
| 523 | /** |
| 524 | * Additional info, including CPU stats. |
| 525 | */ |
| 526 | public String info; |
| 527 | |
| 528 | /** |
| 529 | * Create an uninitialized instance of AnrInfo. |
| 530 | */ |
| 531 | public AnrInfo() { |
| 532 | } |
| 533 | |
| 534 | /** |
| 535 | * Create an instance of AnrInfo initialized from a Parcel. |
| 536 | */ |
| 537 | public AnrInfo(Parcel in) { |
| 538 | activity = in.readString(); |
| 539 | cause = in.readString(); |
| 540 | info = in.readString(); |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Save an AnrInfo instance to a parcel. |
| 545 | */ |
| 546 | public void writeToParcel(Parcel dest, int flags) { |
| 547 | dest.writeString(activity); |
| 548 | dest.writeString(cause); |
| 549 | dest.writeString(info); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Dump an AnrInfo instance to a Printer. |
| 554 | */ |
| 555 | public void dump(Printer pw, String prefix) { |
| 556 | pw.println(prefix + "activity: " + activity); |
| 557 | pw.println(prefix + "cause: " + cause); |
| 558 | pw.println(prefix + "info: " + info); |
| 559 | } |
| 560 | } |
| 561 | |
Jacek Surazski | 151de3d | 2010-02-26 22:52:44 +0100 | [diff] [blame] | 562 | /** |
| 563 | * Describes a battery usage report. |
| 564 | */ |
| 565 | public static class BatteryInfo { |
| 566 | /** |
| 567 | * Percentage of the battery that was used up by the process. |
| 568 | */ |
| 569 | public int usagePercent; |
| 570 | |
| 571 | /** |
| 572 | * Duration in microseconds over which the process used the above |
| 573 | * percentage of battery. |
| 574 | */ |
| 575 | public long durationMicros; |
| 576 | |
| 577 | /** |
| 578 | * Dump of various info impacting battery use. |
| 579 | */ |
| 580 | public String usageDetails; |
| 581 | |
| 582 | /** |
| 583 | * Checkin details. |
| 584 | */ |
| 585 | public String checkinDetails; |
| 586 | |
| 587 | /** |
| 588 | * Create an uninitialized instance of BatteryInfo. |
| 589 | */ |
| 590 | public BatteryInfo() { |
| 591 | } |
| 592 | |
| 593 | /** |
| 594 | * Create an instance of BatteryInfo initialized from a Parcel. |
| 595 | */ |
| 596 | public BatteryInfo(Parcel in) { |
| 597 | usagePercent = in.readInt(); |
| 598 | durationMicros = in.readLong(); |
| 599 | usageDetails = in.readString(); |
| 600 | checkinDetails = in.readString(); |
| 601 | } |
| 602 | |
| 603 | /** |
| 604 | * Save a BatteryInfo instance to a parcel. |
| 605 | */ |
| 606 | public void writeToParcel(Parcel dest, int flags) { |
| 607 | dest.writeInt(usagePercent); |
| 608 | dest.writeLong(durationMicros); |
| 609 | dest.writeString(usageDetails); |
| 610 | dest.writeString(checkinDetails); |
| 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Dump a BatteryInfo instance to a Printer. |
| 615 | */ |
| 616 | public void dump(Printer pw, String prefix) { |
| 617 | pw.println(prefix + "usagePercent: " + usagePercent); |
| 618 | pw.println(prefix + "durationMicros: " + durationMicros); |
| 619 | pw.println(prefix + "usageDetails: " + usageDetails); |
| 620 | pw.println(prefix + "checkinDetails: " + checkinDetails); |
| 621 | } |
| 622 | } |
| 623 | |
Dianne Hackborn | 14bfa39 | 2010-07-24 19:58:06 -0700 | [diff] [blame] | 624 | /** |
| 625 | * Describes a running service report. |
| 626 | */ |
| 627 | public static class RunningServiceInfo { |
| 628 | /** |
| 629 | * Duration in milliseconds that the service has been running. |
| 630 | */ |
| 631 | public long durationMillis; |
| 632 | |
| 633 | /** |
| 634 | * Dump of debug information about the service. |
| 635 | */ |
| 636 | public String serviceDetails; |
| 637 | |
| 638 | /** |
| 639 | * Create an uninitialized instance of RunningServiceInfo. |
| 640 | */ |
| 641 | public RunningServiceInfo() { |
| 642 | } |
| 643 | |
| 644 | /** |
| 645 | * Create an instance of RunningServiceInfo initialized from a Parcel. |
| 646 | */ |
| 647 | public RunningServiceInfo(Parcel in) { |
| 648 | durationMillis = in.readLong(); |
| 649 | serviceDetails = in.readString(); |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * Save a RunningServiceInfo instance to a parcel. |
| 654 | */ |
| 655 | public void writeToParcel(Parcel dest, int flags) { |
| 656 | dest.writeLong(durationMillis); |
| 657 | dest.writeString(serviceDetails); |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * Dump a BatteryInfo instance to a Printer. |
| 662 | */ |
| 663 | public void dump(Printer pw, String prefix) { |
| 664 | pw.println(prefix + "durationMillis: " + durationMillis); |
| 665 | pw.println(prefix + "serviceDetails: " + serviceDetails); |
| 666 | } |
| 667 | } |
| 668 | |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 669 | public static final Parcelable.Creator<ApplicationErrorReport> CREATOR |
| 670 | = new Parcelable.Creator<ApplicationErrorReport>() { |
| 671 | public ApplicationErrorReport createFromParcel(Parcel source) { |
| 672 | return new ApplicationErrorReport(source); |
| 673 | } |
| 674 | |
| 675 | public ApplicationErrorReport[] newArray(int size) { |
| 676 | return new ApplicationErrorReport[size]; |
| 677 | } |
| 678 | }; |
| 679 | |
| 680 | public int describeContents() { |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * Dump the report to a Printer. |
| 686 | */ |
| 687 | public void dump(Printer pw, String prefix) { |
| 688 | pw.println(prefix + "type: " + type); |
| 689 | pw.println(prefix + "packageName: " + packageName); |
| 690 | pw.println(prefix + "installerPackageName: " + installerPackageName); |
| 691 | pw.println(prefix + "processName: " + processName); |
| 692 | pw.println(prefix + "time: " + time); |
Jacek Surazski | e0ee6ef | 2010-01-07 16:23:03 +0100 | [diff] [blame] | 693 | pw.println(prefix + "systemApp: " + systemApp); |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 694 | |
| 695 | switch (type) { |
| 696 | case TYPE_CRASH: |
| 697 | crashInfo.dump(pw, prefix); |
| 698 | break; |
| 699 | case TYPE_ANR: |
| 700 | anrInfo.dump(pw, prefix); |
| 701 | break; |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 702 | case TYPE_BATTERY: |
Jacek Surazski | 151de3d | 2010-02-26 22:52:44 +0100 | [diff] [blame] | 703 | batteryInfo.dump(pw, prefix); |
Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 704 | break; |
Jacek Surazski | 87d0b2f | 2010-08-02 13:03:35 +0200 | [diff] [blame] | 705 | case TYPE_RUNNING_SERVICE: |
| 706 | runningServiceInfo.dump(pw, prefix); |
| 707 | break; |
Jacek Surazski | f5b9c72 | 2009-05-18 12:09:59 +0200 | [diff] [blame] | 708 | } |
| 709 | } |
| 710 | } |