blob: e7a474fde40ce3f9128b5f90d9988349081cd076 [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
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 Jinb592e3b2018-02-01 15:17:04 -080016#pragma once
Joe Onorato1754d742016-11-21 17:51:35 -080017
Joe Onorato99598ee2019-02-11 15:55:13 +000018#include "FdBuffer.h"
19#include "Throttler.h"
20#include "WorkDirectory.h"
Joe Onorato1754d742016-11-21 17:51:35 -080021
Joe Onorato99598ee2019-02-11 15:55:13 +000022#include "frameworks/base/core/proto/android/os/metadata.pb.h"
23#include <android/content/ComponentName.h>
Joe Onorato1754d742016-11-21 17:51:35 -080024#include <android/os/IIncidentReportStatusListener.h>
25#include <android/os/IncidentReportArgs.h>
Joe Onorato99598ee2019-02-11 15:55:13 +000026#include <android/util/protobuf.h>
Joe Onorato1754d742016-11-21 17:51:35 -080027
Yi Jin329130b2018-02-09 16:47:47 -080028#include <map>
Joe Onorato1754d742016-11-21 17:51:35 -080029#include <string>
30#include <vector>
31
32#include <time.h>
Joe Onorato99598ee2019-02-11 15:55:13 +000033#include <stdarg.h>
Yi Jin4e843102018-02-14 15:36:18 -080034
Yi Jin6cacbcb2018-03-30 14:04:52 -070035namespace android {
36namespace os {
37namespace incidentd {
Joe Onorato1754d742016-11-21 17:51:35 -080038
Joe Onorato99598ee2019-02-11 15:55:13 +000039using namespace std;
40using namespace android::content;
41using namespace android::os;
42
43class Section;
44
Joe Onorato1754d742016-11-21 17:51:35 -080045// ================================================================================
Joe Onorato99598ee2019-02-11 15:55:13 +000046class ReportRequest : public virtual RefBase {
47public:
Joe Onorato1754d742016-11-21 17:51:35 -080048 IncidentReportArgs args;
Joe Onorato1754d742016-11-21 17:51:35 -080049
Yi Jinb592e3b2018-02-01 15:17:04 -080050 ReportRequest(const IncidentReportArgs& args, const sp<IIncidentReportStatusListener>& listener,
51 int fd);
Joe Onorato1754d742016-11-21 17:51:35 -080052 virtual ~ReportRequest();
Yi Jinedfd5bb2017-09-06 17:09:11 -070053
Joe Onorato99598ee2019-02-11 15:55:13 +000054 bool isStreaming() { return mIsStreaming; }
55
56 void setStatus(status_t err) { mStatus = err; }
57 status_t getStatus() const { return mStatus; }
58
Yi Jinb592e3b2018-02-01 15:17:04 -080059 bool ok(); // returns true if the request is ok for write.
Joe Onorato99598ee2019-02-11 15:55:13 +000060
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
71private:
72 sp<IIncidentReportStatusListener> mListener;
73 int mFd;
74 bool mIsStreaming;
75 status_t mStatus;
Joe Onorato1754d742016-11-21 17:51:35 -080076};
77
78// ================================================================================
Joe Onorato99598ee2019-02-11 15:55:13 +000079class ReportBatch : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -080080public:
Joe Onorato99598ee2019-02-11 15:55:13 +000081 ReportBatch();
82 virtual ~ReportBatch();
Joe Onorato1754d742016-11-21 17:51:35 -080083
Joe Onorato99598ee2019-02-11 15:55:13 +000084 // 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 Onorato1754d742016-11-21 17:51:35 -080087
Joe Onorato99598ee2019-02-11 15:55:13 +000088 /**
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 Onorato1754d742016-11-21 17:51:35 -080093
Joe Onorato99598ee2019-02-11 15:55:13 +000094 /**
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 Onorato1754d742016-11-21 17:51:35 -080099
Joe Onorato99598ee2019-02-11 15:55:13 +0000100 /**
101 * Returns whether both queues are empty.
102 */
103 bool empty() const;
Yi Jin329130b2018-02-09 16:47:47 -0800104
Joe Onorato99598ee2019-02-11 15:55:13 +0000105 /**
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 Jin329130b2018-02-09 16:47:47 -0800150 bool containsSection(int id);
Joe Onorato99598ee2019-02-11 15:55:13 +0000151
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 Jin329130b2018-02-09 16:47:47 -0800167
Joe Onorato1754d742016-11-21 17:51:35 -0800168private:
Joe Onorato99598ee2019-02-11 15:55:13 +0000169 map<ComponentName, sp<ReportRequest>> mPersistedRequests;
170 vector<sp<ReportRequest>> mStreamingRequests;
171};
Yi Jin329130b2018-02-09 16:47:47 -0800172
Joe Onorato99598ee2019-02-11 15:55:13 +0000173// ================================================================================
174class ReportWriter {
175public:
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
192private:
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 Onorato1754d742016-11-21 17:51:35 -0800238};
239
240// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800241class Reporter : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800242public:
Joe Onorato99598ee2019-02-11 15:55:13 +0000243 Reporter(const sp<WorkDirectory>& workDirectory, const sp<ReportBatch>& batch);
Joe Onorato1754d742016-11-21 17:51:35 -0800244
Joe Onorato1754d742016-11-21 17:51:35 -0800245 virtual ~Reporter();
246
247 // Run the report as described in the batch and args parameters.
Joe Onorato99598ee2019-02-11 15:55:13 +0000248 void runReport(size_t* reportByteSize);
Joe Onorato1754d742016-11-21 17:51:35 -0800249
250private:
Joe Onorato99598ee2019-02-11 15:55:13 +0000251 sp<WorkDirectory> mWorkDirectory;
252 ReportWriter mWriter;
253 sp<ReportBatch> mBatch;
254 sp<ReportFile> mPersistedFile;
Yi Jinadd11e92017-07-30 16:10:07 -0700255
Joe Onorato99598ee2019-02-11 15:55:13 +0000256 void cancel_and_remove_failed_requests();
Joe Onorato1754d742016-11-21 17:51:35 -0800257};
258
Yi Jin6cacbcb2018-03-30 14:04:52 -0700259} // namespace incidentd
260} // namespace os
261} // namespace android