Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 16 | #pragma once |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 17 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 18 | #include "FdBuffer.h" |
| 19 | #include "Throttler.h" |
| 20 | #include "WorkDirectory.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 21 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 22 | #include "frameworks/base/core/proto/android/os/metadata.pb.h" |
| 23 | #include <android/content/ComponentName.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 24 | #include <android/os/IIncidentReportStatusListener.h> |
| 25 | #include <android/os/IncidentReportArgs.h> |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 26 | #include <android/util/protobuf.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 27 | |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 28 | #include <map> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 29 | #include <string> |
| 30 | #include <vector> |
| 31 | |
| 32 | #include <time.h> |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 33 | #include <stdarg.h> |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 34 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 35 | namespace android { |
| 36 | namespace os { |
| 37 | namespace incidentd { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 38 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 39 | using namespace std; |
| 40 | using namespace android::content; |
| 41 | using namespace android::os; |
| 42 | |
| 43 | class Section; |
| 44 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 45 | // ================================================================================ |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 46 | class ReportRequest : public virtual RefBase { |
| 47 | public: |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 48 | IncidentReportArgs args; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 49 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 50 | ReportRequest(const IncidentReportArgs& args, const sp<IIncidentReportStatusListener>& listener, |
| 51 | int fd); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 52 | virtual ~ReportRequest(); |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 53 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 54 | bool isStreaming() { return mIsStreaming; } |
| 55 | |
| 56 | void setStatus(status_t err) { mStatus = err; } |
| 57 | status_t getStatus() const { return mStatus; } |
| 58 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 59 | bool ok(); // returns true if the request is ok for write. |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 60 | |
| 61 | bool containsSection(int sectionId) const { return args.containsSection(sectionId); } |
| 62 | |
| 63 | sp<IIncidentReportStatusListener> getListener() { return mListener; } |
| 64 | |
| 65 | int getFd() { return mFd; } |
| 66 | |
| 67 | int setPersistedFd(int fd); |
| 68 | |
| 69 | void closeFd(); |
| 70 | |
| 71 | private: |
| 72 | sp<IIncidentReportStatusListener> mListener; |
| 73 | int mFd; |
| 74 | bool mIsStreaming; |
| 75 | status_t mStatus; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 76 | }; |
| 77 | |
| 78 | // ================================================================================ |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 79 | class ReportBatch : public virtual RefBase { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 80 | public: |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 81 | ReportBatch(); |
| 82 | virtual ~ReportBatch(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 83 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 84 | // TODO: Should there be some kind of listener associated with the |
| 85 | // component? Could be good for getting status updates e.g. in the ui, |
| 86 | // as it progresses. But that's out of scope for now. |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 87 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 88 | /** |
| 89 | * Schedule a report for the "main" report, where it will be delivered to |
| 90 | * the uploaders and/or dropbox. |
| 91 | */ |
| 92 | void addPersistedReport(const IncidentReportArgs& args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 93 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 94 | /** |
| 95 | * Adds a ReportRequest to the queue for one that has a listener an and fd |
| 96 | */ |
| 97 | void addStreamingReport(const IncidentReportArgs& args, |
| 98 | const sp<IIncidentReportStatusListener>& listener, int streamFd); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 99 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 100 | /** |
| 101 | * Returns whether both queues are empty. |
| 102 | */ |
| 103 | bool empty() const; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 104 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 105 | /** |
| 106 | * Returns whether there are any persisted records. |
| 107 | */ |
| 108 | bool hasPersistedReports() const { return mPersistedRequests.size() > 0; } |
| 109 | |
| 110 | /** |
| 111 | * Return the persisted request for the given component, or nullptr. |
| 112 | */ |
| 113 | sp<ReportRequest> getPersistedRequest(const ComponentName& component); |
| 114 | |
| 115 | /** |
| 116 | * Call func(request) for each Request. |
| 117 | */ |
| 118 | void forEachPersistedRequest(const function<void (const sp<ReportRequest>&)>& func); |
| 119 | |
| 120 | /** |
| 121 | * Call func(request) for each Request. |
| 122 | */ |
| 123 | void forEachStreamingRequest(const function<void (const sp<ReportRequest>&)>& func); |
| 124 | |
| 125 | /** |
| 126 | * Call func(request) for each file descriptor that has |
| 127 | */ |
| 128 | void forEachFd(int sectionId, const function<void (const sp<ReportRequest>&)>& func); |
| 129 | |
| 130 | /** |
| 131 | * Call func(listener) for every listener in this batch. |
| 132 | */ |
| 133 | void forEachListener(const function<void (const sp<IIncidentReportStatusListener>&)>& func); |
| 134 | |
| 135 | /** |
| 136 | * Call func(listener) for every listener in this batch that requests |
| 137 | * sectionId. |
| 138 | */ |
| 139 | void forEachListener(int sectionId, |
| 140 | const function<void (const sp<IIncidentReportStatusListener>&)>& func); |
| 141 | /** |
| 142 | * Get an IncidentReportArgs that represents the combined args for the |
| 143 | * persisted requests. |
| 144 | */ |
| 145 | void getCombinedPersistedArgs(IncidentReportArgs* results); |
| 146 | |
| 147 | /** |
| 148 | * Return whether any of the requests contain the section. |
| 149 | */ |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 150 | bool containsSection(int id); |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 151 | |
| 152 | /** |
| 153 | * Remove all of the broadcast (persisted) requests. |
| 154 | */ |
| 155 | void clearPersistedRequests(); |
| 156 | |
| 157 | /** |
| 158 | * Get the requests that have encountered errors. |
| 159 | */ |
| 160 | void getFailedRequests(vector<sp<ReportRequest>>* requests); |
| 161 | |
| 162 | /** |
| 163 | * Remove the request from whichever list it's in. |
| 164 | */ |
| 165 | void removeRequest(const sp<ReportRequest>& request); |
| 166 | |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 167 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 168 | private: |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 169 | map<ComponentName, sp<ReportRequest>> mPersistedRequests; |
| 170 | vector<sp<ReportRequest>> mStreamingRequests; |
| 171 | }; |
Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame] | 172 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 173 | // ================================================================================ |
| 174 | class ReportWriter { |
| 175 | public: |
| 176 | ReportWriter(const sp<ReportBatch>& batch); |
| 177 | ~ReportWriter(); |
| 178 | |
| 179 | void setPersistedFile(sp<ReportFile> file); |
| 180 | void setMaxPersistedPrivacyPolicy(uint8_t privacyPolicy); |
| 181 | |
| 182 | void startSection(int sectionId); |
| 183 | void endSection(IncidentMetadata::SectionStats* sectionStats); |
| 184 | |
| 185 | void setSectionStats(const FdBuffer& buffer); |
| 186 | |
| 187 | void warning(const Section* section, status_t err, const char* format, ...); |
| 188 | void error(const Section* section, status_t err, const char* format, ...); |
| 189 | |
| 190 | status_t writeSection(const FdBuffer& buffer); |
| 191 | |
| 192 | private: |
| 193 | // Data about all requests |
| 194 | sp<ReportBatch> mBatch; |
| 195 | |
| 196 | /** |
| 197 | * The file on disk where we will store the persisted file. |
| 198 | */ |
| 199 | sp<ReportFile> mPersistedFile; |
| 200 | |
| 201 | /** |
| 202 | * The least restricted privacy policy of all of the perstited |
| 203 | * requests. We pre-filter to that to save disk space. |
| 204 | */ |
| 205 | uint8_t mMaxPersistedPrivacyPolicy; |
| 206 | |
| 207 | /** |
| 208 | * The current section that is being written. |
| 209 | */ |
| 210 | int mCurrentSectionId; |
| 211 | |
| 212 | /** |
| 213 | * The time that that the current section was started. |
| 214 | */ |
| 215 | int64_t mSectionStartTimeMs; |
| 216 | |
| 217 | /** |
| 218 | * The last section that setSectionStats was called for, so if someone misses |
| 219 | * it we can log that. |
| 220 | */ |
| 221 | int mSectionStatsCalledForSectionId; |
| 222 | |
| 223 | /* |
| 224 | * Fields for IncidentMetadata.SectionStats. Set by setSectionStats. Accessed by |
| 225 | * getSectionStats. |
| 226 | */ |
| 227 | int32_t mDumpSizeBytes; |
| 228 | int64_t mDumpDurationMs; |
| 229 | bool mSectionTimedOut; |
| 230 | bool mSectionTruncated; |
| 231 | bool mSectionBufferSuccess; |
| 232 | bool mHadError; |
| 233 | string mSectionErrors; |
| 234 | size_t mMaxSectionDataFilteredSize; |
| 235 | |
| 236 | void vflog(const Section* section, status_t err, int level, const char* levelText, |
| 237 | const char* format, va_list args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 238 | }; |
| 239 | |
| 240 | // ================================================================================ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 241 | class Reporter : public virtual RefBase { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 242 | public: |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 243 | Reporter(const sp<WorkDirectory>& workDirectory, const sp<ReportBatch>& batch); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 244 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 245 | virtual ~Reporter(); |
| 246 | |
| 247 | // Run the report as described in the batch and args parameters. |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 248 | void runReport(size_t* reportByteSize); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 249 | |
| 250 | private: |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 251 | sp<WorkDirectory> mWorkDirectory; |
| 252 | ReportWriter mWriter; |
| 253 | sp<ReportBatch> mBatch; |
| 254 | sp<ReportFile> mPersistedFile; |
Yi Jin | add11e9 | 2017-07-30 16:10:07 -0700 | [diff] [blame] | 255 | |
Joe Onorato | 99598ee | 2019-02-11 15:55:13 +0000 | [diff] [blame^] | 256 | void cancel_and_remove_failed_requests(); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 257 | }; |
| 258 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 259 | } // namespace incidentd |
| 260 | } // namespace os |
| 261 | } // namespace android |