Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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.os; |
| 18 | |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 19 | import android.annotation.CallbackExecutor; |
Abhijeet Kaur | 0bbb9f6 | 2019-03-08 11:00:29 +0000 | [diff] [blame] | 20 | import android.annotation.FloatRange; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 21 | import android.annotation.IntDef; |
| 22 | import android.annotation.NonNull; |
| 23 | import android.annotation.Nullable; |
| 24 | import android.annotation.RequiresPermission; |
Nandana Dutt | 2083e8c | 2019-01-23 20:02:29 +0000 | [diff] [blame] | 25 | import android.annotation.SystemApi; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 26 | import android.annotation.SystemService; |
Nandana Dutt | d7e8d5a | 2019-04-11 17:27:45 +0100 | [diff] [blame] | 27 | import android.annotation.TestApi; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 28 | import android.content.Context; |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 29 | import android.util.Log; |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 30 | |
| 31 | import com.android.internal.util.Preconditions; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 32 | |
Nandana Dutt | 9d6ff29 | 2019-03-13 16:42:17 +0000 | [diff] [blame] | 33 | import libcore.io.IoUtils; |
| 34 | |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 35 | import java.io.File; |
Abhijeet Kaur | 006a6bb | 2019-04-16 18:28:20 +0100 | [diff] [blame] | 36 | import java.io.FileNotFoundException; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 37 | import java.lang.annotation.Retention; |
| 38 | import java.lang.annotation.RetentionPolicy; |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 39 | import java.util.concurrent.Executor; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Class that provides a privileged API to capture and consume bugreports. |
| 43 | * |
| 44 | * @hide |
| 45 | */ |
Nandana Dutt | 2083e8c | 2019-01-23 20:02:29 +0000 | [diff] [blame] | 46 | @SystemApi |
Nandana Dutt | d7e8d5a | 2019-04-11 17:27:45 +0100 | [diff] [blame] | 47 | @TestApi |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 48 | @SystemService(Context.BUGREPORT_SERVICE) |
Abhijeet Kaur | 0bbb9f6 | 2019-03-08 11:00:29 +0000 | [diff] [blame] | 49 | public final class BugreportManager { |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 50 | |
| 51 | private static final String TAG = "BugreportManager"; |
| 52 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 53 | private final Context mContext; |
| 54 | private final IDumpstate mBinder; |
| 55 | |
| 56 | /** @hide */ |
| 57 | public BugreportManager(@NonNull Context context, IDumpstate binder) { |
| 58 | mContext = context; |
| 59 | mBinder = binder; |
| 60 | } |
| 61 | |
| 62 | /** |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 63 | * An interface describing the callback for bugreport progress and status. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 64 | */ |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 65 | public abstract static class BugreportCallback { |
| 66 | /** @hide */ |
| 67 | @Retention(RetentionPolicy.SOURCE) |
| 68 | @IntDef(prefix = { "BUGREPORT_ERROR_" }, value = { |
| 69 | BUGREPORT_ERROR_INVALID_INPUT, |
| 70 | BUGREPORT_ERROR_RUNTIME, |
| 71 | BUGREPORT_ERROR_USER_DENIED_CONSENT, |
Nandana Dutt | cfb3d48 | 2019-02-20 11:25:35 +0000 | [diff] [blame] | 72 | BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT, |
| 73 | BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 74 | }) |
| 75 | |
| 76 | /** Possible error codes taking a bugreport can encounter */ |
| 77 | public @interface BugreportErrorCode {} |
| 78 | |
| 79 | /** The input options were invalid */ |
| 80 | public static final int BUGREPORT_ERROR_INVALID_INPUT = |
| 81 | IDumpstateListener.BUGREPORT_ERROR_INVALID_INPUT; |
| 82 | |
| 83 | /** A runtime error occured */ |
| 84 | public static final int BUGREPORT_ERROR_RUNTIME = |
| 85 | IDumpstateListener.BUGREPORT_ERROR_RUNTIME_ERROR; |
| 86 | |
| 87 | /** User denied consent to share the bugreport */ |
| 88 | public static final int BUGREPORT_ERROR_USER_DENIED_CONSENT = |
| 89 | IDumpstateListener.BUGREPORT_ERROR_USER_DENIED_CONSENT; |
| 90 | |
| 91 | /** The request to get user consent timed out. */ |
| 92 | public static final int BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT = |
| 93 | IDumpstateListener.BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT; |
| 94 | |
Nandana Dutt | cfb3d48 | 2019-02-20 11:25:35 +0000 | [diff] [blame] | 95 | /** There is currently a bugreport running. The caller should try again later. */ |
| 96 | public static final int BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS = |
| 97 | IDumpstateListener.BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS; |
| 98 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 99 | /** |
| 100 | * Called when there is a progress update. |
| 101 | * @param progress the progress in [0.0, 100.0] |
| 102 | */ |
Abhijeet Kaur | 0bbb9f6 | 2019-03-08 11:00:29 +0000 | [diff] [blame] | 103 | public void onProgress(@FloatRange(from = 0f, to = 100f) float progress) {} |
Nandana Dutt | bba7e82 | 2019-01-23 19:11:01 +0000 | [diff] [blame] | 104 | |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 105 | /** |
| 106 | * Called when taking bugreport resulted in an error. |
| 107 | * |
Nandana Dutt | bba7e82 | 2019-01-23 19:11:01 +0000 | [diff] [blame] | 108 | * <p>If {@code BUGREPORT_ERROR_USER_DENIED_CONSENT} is passed, then the user did not |
| 109 | * consent to sharing the bugreport with the calling app. |
| 110 | * |
| 111 | * <p>If {@code BUGREPORT_ERROR_USER_CONSENT_TIMED_OUT} is passed, then the consent timed |
| 112 | * out, but the bugreport could be available in the internal directory of dumpstate for |
| 113 | * manual retrieval. |
Nandana Dutt | cfb3d48 | 2019-02-20 11:25:35 +0000 | [diff] [blame] | 114 | * |
| 115 | * <p> If {@code BUGREPORT_ERROR_ANOTHER_REPORT_IN_PROGRESS} is passed, then the |
| 116 | * caller should try later, as only one bugreport can be in progress at a time. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 117 | */ |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 118 | public void onError(@BugreportErrorCode int errorCode) {} |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 119 | |
| 120 | /** |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 121 | * Called when taking bugreport finishes successfully. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 122 | */ |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 123 | public void onFinished() {} |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 127 | * Starts a bugreport. |
| 128 | * |
| 129 | * <p>This starts a bugreport in the background. However the call itself can take several |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 130 | * seconds to return in the worst case. {@code callback} will receive progress and status |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 131 | * updates. |
| 132 | * |
| 133 | * <p>The bugreport artifacts will be copied over to the given file descriptors only if the |
| 134 | * user consents to sharing with the calling app. |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 135 | * |
Nandana Dutt | 9d6ff29 | 2019-03-13 16:42:17 +0000 | [diff] [blame] | 136 | * <p>{@link BugreportManager} takes ownership of {@code bugreportFd} and {@code screenshotFd}. |
| 137 | * |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 138 | * @param bugreportFd file to write the bugreport. This should be opened in write-only, |
| 139 | * append mode. |
| 140 | * @param screenshotFd file to write the screenshot, if necessary. This should be opened |
| 141 | * in write-only, append mode. |
| 142 | * @param params options that specify what kind of a bugreport should be taken |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 143 | * @param callback callback for progress and status updates |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 144 | */ |
| 145 | @RequiresPermission(android.Manifest.permission.DUMP) |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 146 | public void startBugreport(@NonNull ParcelFileDescriptor bugreportFd, |
| 147 | @Nullable ParcelFileDescriptor screenshotFd, |
| 148 | @NonNull BugreportParams params, |
| 149 | @NonNull @CallbackExecutor Executor executor, |
| 150 | @NonNull BugreportCallback callback) { |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 151 | try { |
Nandana Dutt | 9d6ff29 | 2019-03-13 16:42:17 +0000 | [diff] [blame] | 152 | Preconditions.checkNotNull(bugreportFd); |
| 153 | Preconditions.checkNotNull(params); |
| 154 | Preconditions.checkNotNull(executor); |
| 155 | Preconditions.checkNotNull(callback); |
| 156 | |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 157 | if (screenshotFd == null) { |
| 158 | // Binder needs a valid File Descriptor to be passed |
Abhijeet Kaur | 006a6bb | 2019-04-16 18:28:20 +0100 | [diff] [blame] | 159 | screenshotFd = ParcelFileDescriptor.open(new File("/dev/null"), |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 160 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 161 | } |
Abhijeet Kaur | 006a6bb | 2019-04-16 18:28:20 +0100 | [diff] [blame] | 162 | DumpstateListener dsListener = new DumpstateListener(executor, callback); |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 163 | // Note: mBinder can get callingUid from the binder transaction. |
| 164 | mBinder.startBugreport(-1 /* callingUid */, |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 165 | mContext.getOpPackageName(), |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 166 | bugreportFd.getFileDescriptor(), |
Abhijeet Kaur | 86311f8 | 2019-03-29 17:04:48 +0000 | [diff] [blame] | 167 | screenshotFd.getFileDescriptor(), |
Nandana Dutt | 161a446 | 2019-01-16 18:18:38 +0000 | [diff] [blame] | 168 | params.getMode(), dsListener); |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 169 | } catch (RemoteException e) { |
| 170 | throw e.rethrowFromSystemServer(); |
Abhijeet Kaur | 006a6bb | 2019-04-16 18:28:20 +0100 | [diff] [blame] | 171 | } catch (FileNotFoundException e) { |
| 172 | Log.wtf(TAG, "Not able to find /dev/null file: ", e); |
Nandana Dutt | 9d6ff29 | 2019-03-13 16:42:17 +0000 | [diff] [blame] | 173 | } finally { |
| 174 | // We can close the file descriptors here because binder would have duped them. |
| 175 | IoUtils.closeQuietly(bugreportFd); |
| 176 | if (screenshotFd != null) { |
| 177 | IoUtils.closeQuietly(screenshotFd); |
| 178 | } |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 182 | /* |
| 183 | * Cancels a currently running bugreport. |
| 184 | */ |
| 185 | @RequiresPermission(android.Manifest.permission.DUMP) |
| 186 | public void cancelBugreport() { |
| 187 | try { |
| 188 | mBinder.cancelBugreport(); |
| 189 | } catch (RemoteException e) { |
| 190 | throw e.rethrowFromSystemServer(); |
| 191 | } |
| 192 | } |
| 193 | |
Nandana Dutt | 551906c | 2019-01-23 09:51:49 +0000 | [diff] [blame] | 194 | private final class DumpstateListener extends IDumpstateListener.Stub { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 195 | private final Executor mExecutor; |
| 196 | private final BugreportCallback mCallback; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 197 | |
Abhijeet Kaur | 006a6bb | 2019-04-16 18:28:20 +0100 | [diff] [blame] | 198 | DumpstateListener(Executor executor, BugreportCallback callback) { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 199 | mExecutor = executor; |
| 200 | mCallback = callback; |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 204 | public void onProgress(int progress) throws RemoteException { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 205 | final long identity = Binder.clearCallingIdentity(); |
| 206 | try { |
| 207 | mExecutor.execute(() -> { |
| 208 | mCallback.onProgress(progress); |
| 209 | }); |
| 210 | } finally { |
| 211 | Binder.restoreCallingIdentity(identity); |
| 212 | } |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | @Override |
| 216 | public void onError(int errorCode) throws RemoteException { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 217 | final long identity = Binder.clearCallingIdentity(); |
| 218 | try { |
| 219 | mExecutor.execute(() -> { |
| 220 | mCallback.onError(errorCode); |
| 221 | }); |
| 222 | } finally { |
| 223 | Binder.restoreCallingIdentity(identity); |
| 224 | } |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 228 | public void onFinished() throws RemoteException { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 229 | final long identity = Binder.clearCallingIdentity(); |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 230 | try { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 231 | mExecutor.execute(() -> { |
| 232 | mCallback.onFinished(); |
| 233 | }); |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 234 | } finally { |
Nandana Dutt | 28d8dd7 | 2019-01-25 14:31:05 +0000 | [diff] [blame] | 235 | Binder.restoreCallingIdentity(identity); |
Nandana Dutt | b2da22a | 2019-01-23 08:39:05 +0000 | [diff] [blame] | 236 | } |
Nandana Dutt | 432f8c7 | 2019-01-14 17:39:13 +0000 | [diff] [blame] | 237 | } |
Nandana Dutt | 3386fb7 | 2018-12-12 17:26:57 +0000 | [diff] [blame] | 238 | } |
| 239 | } |