blob: 334d77c1494e4229f2879078cde2728ce7dcaa92 [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 Jin4e843102018-02-14 15:36:18 -080016#define DEBUG false
Yi Jinb592e3b2018-02-01 15:17:04 -080017#include "Log.h"
Joe Onorato1754d742016-11-21 17:51:35 -080018
19#include "Section.h"
Yi Jin99c248f2017-08-25 18:11:58 -070020
Yi Jin3c034c92017-12-22 17:36:47 -080021#include <wait.h>
22
Yi Jin3c034c92017-12-22 17:36:47 -080023#include <mutex>
Joe Onorato1754d742016-11-21 17:51:35 -080024
Yi Jinb592e3b2018-02-01 15:17:04 -080025#include <android-base/file.h>
Yi Jinc23fad22017-09-15 17:24:59 -070026#include <android/util/protobuf.h>
Joe Onorato1754d742016-11-21 17:51:35 -080027#include <binder/IServiceManager.h>
Yi Jin3c034c92017-12-22 17:36:47 -080028#include <log/log_event_list.h>
Yi Jin3c034c92017-12-22 17:36:47 -080029#include <log/log_read.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080030#include <log/logprint.h>
Yi Jin3c034c92017-12-22 17:36:47 -080031#include <private/android_logger.h>
32
33#include "FdBuffer.h"
Yi Jin3c034c92017-12-22 17:36:47 -080034#include "Privacy.h"
35#include "PrivacyBuffer.h"
Yi Jin1a11fa12018-02-22 16:44:10 -080036#include "frameworks/base/core/proto/android/os/data.proto.h"
Yi Jinb592e3b2018-02-01 15:17:04 -080037#include "frameworks/base/core/proto/android/util/log.proto.h"
38#include "incidentd_util.h"
Joe Onorato1754d742016-11-21 17:51:35 -080039
Yi Jinb592e3b2018-02-01 15:17:04 -080040using namespace android::base;
Yi Jinc23fad22017-09-15 17:24:59 -070041using namespace android::util;
Joe Onorato1754d742016-11-21 17:51:35 -080042using namespace std;
43
Yi Jinc23fad22017-09-15 17:24:59 -070044// special section ids
45const int FIELD_ID_INCIDENT_HEADER = 1;
Yi Jin329130b2018-02-09 16:47:47 -080046const int FIELD_ID_INCIDENT_METADATA = 2;
Yi Jinc23fad22017-09-15 17:24:59 -070047
48// incident section parameters
Yi Jinb592e3b2018-02-01 15:17:04 -080049const int WAIT_MAX = 5;
Yi Jinb44f7d42017-07-21 12:12:59 -070050const struct timespec WAIT_INTERVAL_NS = {0, 200 * 1000 * 1000};
Yi Jin3c034c92017-12-22 17:36:47 -080051const char INCIDENT_HELPER[] = "/system/bin/incident_helper";
Yi Jin1a11fa12018-02-22 16:44:10 -080052const char GZIP[] = "/system/bin/gzip";
Yi Jinb44f7d42017-07-21 12:12:59 -070053
Yi Jin1a11fa12018-02-22 16:44:10 -080054static pid_t fork_execute_incident_helper(const int id, Fpipe* p2cPipe, Fpipe* c2pPipe) {
Yi Jinb592e3b2018-02-01 15:17:04 -080055 const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL};
Yi Jin1a11fa12018-02-22 16:44:10 -080056 return fork_execute_cmd(INCIDENT_HELPER, const_cast<char**>(ihArgs), p2cPipe, c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -070057}
58
Yi Jin99c248f2017-08-25 18:11:58 -070059// ================================================================================
Yi Jin4bab3a12018-01-10 16:50:59 -080060static status_t statusCode(int status) {
61 if (WIFSIGNALED(status)) {
Yi Jinb592e3b2018-02-01 15:17:04 -080062 VLOG("return by signal: %s", strerror(WTERMSIG(status)));
63 return -WTERMSIG(status);
Yi Jin4bab3a12018-01-10 16:50:59 -080064 } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -080065 VLOG("return by exit: %s", strerror(WEXITSTATUS(status)));
66 return -WEXITSTATUS(status);
Yi Jin4bab3a12018-01-10 16:50:59 -080067 }
68 return NO_ERROR;
69}
70
Yi Jinedfd5bb2017-09-06 17:09:11 -070071static status_t kill_child(pid_t pid) {
Yi Jinb44f7d42017-07-21 12:12:59 -070072 int status;
Yi Jinb592e3b2018-02-01 15:17:04 -080073 VLOG("try to kill child process %d", pid);
Yi Jinb44f7d42017-07-21 12:12:59 -070074 kill(pid, SIGKILL);
75 if (waitpid(pid, &status, 0) == -1) return -1;
Yi Jin4bab3a12018-01-10 16:50:59 -080076 return statusCode(status);
Yi Jinb44f7d42017-07-21 12:12:59 -070077}
78
Yi Jinedfd5bb2017-09-06 17:09:11 -070079static status_t wait_child(pid_t pid) {
Yi Jinb44f7d42017-07-21 12:12:59 -070080 int status;
81 bool died = false;
82 // wait for child to report status up to 1 seconds
Yi Jinb592e3b2018-02-01 15:17:04 -080083 for (int loop = 0; !died && loop < WAIT_MAX; loop++) {
Yi Jinb44f7d42017-07-21 12:12:59 -070084 if (waitpid(pid, &status, WNOHANG) == pid) died = true;
85 // sleep for 0.2 second
86 nanosleep(&WAIT_INTERVAL_NS, NULL);
87 }
Yi Jinedfd5bb2017-09-06 17:09:11 -070088 if (!died) return kill_child(pid);
Yi Jin4bab3a12018-01-10 16:50:59 -080089 return statusCode(status);
Yi Jinb44f7d42017-07-21 12:12:59 -070090}
Joe Onorato1754d742016-11-21 17:51:35 -080091// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -080092static status_t write_section_header(int fd, int sectionId, size_t size) {
Yi Jin99c248f2017-08-25 18:11:58 -070093 uint8_t buf[20];
Yi Jinb592e3b2018-02-01 15:17:04 -080094 uint8_t* p = write_length_delimited_tag_header(buf, sectionId, size);
95 return WriteFully(fd, buf, p - buf) ? NO_ERROR : -errno;
Yi Jin99c248f2017-08-25 18:11:58 -070096}
97
Yi Jinb592e3b2018-02-01 15:17:04 -080098static status_t write_report_requests(const int id, const FdBuffer& buffer,
99 ReportRequestSet* requests) {
Yi Jin0f047162017-09-05 13:44:22 -0700100 status_t err = -EBADF;
Yi Jinc23fad22017-09-15 17:24:59 -0700101 EncodedBuffer::iterator data = buffer.data();
102 PrivacyBuffer privacyBuffer(get_privacy_of_section(id), data);
Yi Jin99c248f2017-08-25 18:11:58 -0700103 int writeable = 0;
Yi Jin329130b2018-02-09 16:47:47 -0800104 auto stats = requests->sectionStats(id);
105
106 stats->set_dump_size_bytes(data.size());
107 stats->set_dump_duration_ms(buffer.durationMs());
108 stats->set_timed_out(buffer.timedOut());
109 stats->set_is_truncated(buffer.truncated());
Yi Jin99c248f2017-08-25 18:11:58 -0700110
Yi Jin0f047162017-09-05 13:44:22 -0700111 // The streaming ones, group requests by spec in order to save unnecessary strip operations
112 map<PrivacySpec, vector<sp<ReportRequest>>> requestsBySpec;
Yi Jin3ec5cc72018-01-26 13:42:43 -0800113 for (auto it = requests->begin(); it != requests->end(); it++) {
Yi Jin99c248f2017-08-25 18:11:58 -0700114 sp<ReportRequest> request = *it;
Yi Jinedfd5bb2017-09-06 17:09:11 -0700115 if (!request->ok() || !request->args.containsSection(id)) {
Yi Jin0f047162017-09-05 13:44:22 -0700116 continue; // skip invalid request
Yi Jin99c248f2017-08-25 18:11:58 -0700117 }
Yi Jin3ec5cc72018-01-26 13:42:43 -0800118 PrivacySpec spec = PrivacySpec::new_spec(request->args.dest());
Yi Jin0f047162017-09-05 13:44:22 -0700119 requestsBySpec[spec].push_back(request);
120 }
121
Yi Jin3ec5cc72018-01-26 13:42:43 -0800122 for (auto mit = requestsBySpec.begin(); mit != requestsBySpec.end(); mit++) {
Yi Jin0f047162017-09-05 13:44:22 -0700123 PrivacySpec spec = mit->first;
Yi Jinc23fad22017-09-15 17:24:59 -0700124 err = privacyBuffer.strip(spec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800125 if (err != NO_ERROR) return err; // it means the privacyBuffer data is corrupted.
Yi Jinc23fad22017-09-15 17:24:59 -0700126 if (privacyBuffer.size() == 0) continue;
Yi Jin0f047162017-09-05 13:44:22 -0700127
Yi Jin3ec5cc72018-01-26 13:42:43 -0800128 for (auto it = mit->second.begin(); it != mit->second.end(); it++) {
Yi Jin0f047162017-09-05 13:44:22 -0700129 sp<ReportRequest> request = *it;
Yi Jinc23fad22017-09-15 17:24:59 -0700130 err = write_section_header(request->fd, id, privacyBuffer.size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800131 if (err != NO_ERROR) {
132 request->err = err;
133 continue;
134 }
Yi Jinc23fad22017-09-15 17:24:59 -0700135 err = privacyBuffer.flush(request->fd);
Yi Jinb592e3b2018-02-01 15:17:04 -0800136 if (err != NO_ERROR) {
137 request->err = err;
138 continue;
139 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700140 writeable++;
Yi Jinb592e3b2018-02-01 15:17:04 -0800141 VLOG("Section %d flushed %zu bytes to fd %d with spec %d", id, privacyBuffer.size(),
142 request->fd, spec.dest);
Yi Jin0f047162017-09-05 13:44:22 -0700143 }
Yi Jinc23fad22017-09-15 17:24:59 -0700144 privacyBuffer.clear();
Yi Jin99c248f2017-08-25 18:11:58 -0700145 }
146
147 // The dropbox file
148 if (requests->mainFd() >= 0) {
Yi Jin329130b2018-02-09 16:47:47 -0800149 PrivacySpec spec = PrivacySpec::new_spec(requests->mainDest());
Yi Jin3ec5cc72018-01-26 13:42:43 -0800150 err = privacyBuffer.strip(spec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800151 if (err != NO_ERROR) return err; // the buffer data is corrupted.
Yi Jinc23fad22017-09-15 17:24:59 -0700152 if (privacyBuffer.size() == 0) goto DONE;
Yi Jin0f047162017-09-05 13:44:22 -0700153
Yi Jinc23fad22017-09-15 17:24:59 -0700154 err = write_section_header(requests->mainFd(), id, privacyBuffer.size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800155 if (err != NO_ERROR) {
156 requests->setMainFd(-1);
157 goto DONE;
158 }
Yi Jinc23fad22017-09-15 17:24:59 -0700159 err = privacyBuffer.flush(requests->mainFd());
Yi Jinb592e3b2018-02-01 15:17:04 -0800160 if (err != NO_ERROR) {
161 requests->setMainFd(-1);
162 goto DONE;
163 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700164 writeable++;
Yi Jinb592e3b2018-02-01 15:17:04 -0800165 VLOG("Section %d flushed %zu bytes to dropbox %d with spec %d", id, privacyBuffer.size(),
166 requests->mainFd(), spec.dest);
Yi Jin329130b2018-02-09 16:47:47 -0800167 stats->set_report_size_bytes(privacyBuffer.size());
Yi Jin99c248f2017-08-25 18:11:58 -0700168 }
Yi Jinedfd5bb2017-09-06 17:09:11 -0700169
170DONE:
Yi Jin99c248f2017-08-25 18:11:58 -0700171 // only returns error if there is no fd to write to.
172 return writeable > 0 ? NO_ERROR : err;
173}
174
175// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800176Section::Section(int i, const int64_t timeoutMs) : id(i), timeoutMs(timeoutMs) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800177
Yi Jinb592e3b2018-02-01 15:17:04 -0800178Section::~Section() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800179
Joe Onorato1754d742016-11-21 17:51:35 -0800180// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800181HeaderSection::HeaderSection() : Section(FIELD_ID_INCIDENT_HEADER, 0) {}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700182
Yi Jinb592e3b2018-02-01 15:17:04 -0800183HeaderSection::~HeaderSection() {}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700184
Yi Jinb592e3b2018-02-01 15:17:04 -0800185status_t HeaderSection::Execute(ReportRequestSet* requests) const {
186 for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700187 const sp<ReportRequest> request = *it;
Yi Jinbdf58942017-11-14 17:58:19 -0800188 const vector<vector<uint8_t>>& headers = request->args.headers();
Yi Jinedfd5bb2017-09-06 17:09:11 -0700189
Yi Jinb592e3b2018-02-01 15:17:04 -0800190 for (vector<vector<uint8_t>>::const_iterator buf = headers.begin(); buf != headers.end();
191 buf++) {
Yi Jinedfd5bb2017-09-06 17:09:11 -0700192 if (buf->empty()) continue;
193
194 // So the idea is only requests with negative fd are written to dropbox file.
195 int fd = request->fd >= 0 ? request->fd : requests->mainFd();
Yi Jin329130b2018-02-09 16:47:47 -0800196 write_section_header(fd, id, buf->size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800197 WriteFully(fd, (uint8_t const*)buf->data(), buf->size());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700198 // If there was an error now, there will be an error later and we will remove
199 // it from the list then.
200 }
201 }
202 return NO_ERROR;
203}
Yi Jin329130b2018-02-09 16:47:47 -0800204// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800205MetadataSection::MetadataSection() : Section(FIELD_ID_INCIDENT_METADATA, 0) {}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700206
Yi Jinb592e3b2018-02-01 15:17:04 -0800207MetadataSection::~MetadataSection() {}
Yi Jin329130b2018-02-09 16:47:47 -0800208
Yi Jinb592e3b2018-02-01 15:17:04 -0800209status_t MetadataSection::Execute(ReportRequestSet* requests) const {
Yi Jin329130b2018-02-09 16:47:47 -0800210 std::string metadataBuf;
211 requests->metadata().SerializeToString(&metadataBuf);
Yi Jinb592e3b2018-02-01 15:17:04 -0800212 for (ReportRequestSet::iterator it = requests->begin(); it != requests->end(); it++) {
Yi Jin329130b2018-02-09 16:47:47 -0800213 const sp<ReportRequest> request = *it;
214 if (metadataBuf.empty() || request->fd < 0 || request->err != NO_ERROR) {
215 continue;
216 }
217 write_section_header(request->fd, id, metadataBuf.size());
Yi Jinb592e3b2018-02-01 15:17:04 -0800218 if (!WriteFully(request->fd, (uint8_t const*)metadataBuf.data(), metadataBuf.size())) {
219 ALOGW("Failed to write metadata to fd %d", request->fd);
220 // we don't fail if we can't write to a single request's fd.
221 }
Yi Jin329130b2018-02-09 16:47:47 -0800222 }
223 if (requests->mainFd() >= 0 && !metadataBuf.empty()) {
224 write_section_header(requests->mainFd(), id, metadataBuf.size());
Yi Jin4e843102018-02-14 15:36:18 -0800225 if (!WriteFully(requests->mainFd(), (uint8_t const*)metadataBuf.data(),
226 metadataBuf.size())) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800227 ALOGW("Failed to write metadata to dropbox fd %d", requests->mainFd());
228 return -1;
229 }
Yi Jin329130b2018-02-09 16:47:47 -0800230 }
231 return NO_ERROR;
232}
Yi Jinedfd5bb2017-09-06 17:09:11 -0700233// ================================================================================
Yi Jin1a11fa12018-02-22 16:44:10 -0800234static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; }
235
Yi Jinb44f7d42017-07-21 12:12:59 -0700236FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Yi Jinb592e3b2018-02-01 15:17:04 -0800237 : Section(id, timeoutMs), mFilename(filename) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700238 name = filename;
Yi Jin1a11fa12018-02-22 16:44:10 -0800239 mIsSysfs = isSysfs(filename);
Yi Jin0a3406f2017-06-22 19:23:11 -0700240}
241
242FileSection::~FileSection() {}
243
Yi Jinb592e3b2018-02-01 15:17:04 -0800244status_t FileSection::Execute(ReportRequestSet* requests) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700245 // read from mFilename first, make sure the file is available
246 // add O_CLOEXEC to make sure it is closed when exec incident helper
George Burgess IV6f9735b2017-08-03 16:08:29 -0700247 int fd = open(mFilename, O_RDONLY | O_CLOEXEC);
Yi Jin0a3406f2017-06-22 19:23:11 -0700248 if (fd == -1) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800249 ALOGW("FileSection '%s' failed to open file", this->name.string());
250 return -errno;
Yi Jin0a3406f2017-06-22 19:23:11 -0700251 }
252
Yi Jinb44f7d42017-07-21 12:12:59 -0700253 FdBuffer buffer;
254 Fpipe p2cPipe;
255 Fpipe c2pPipe;
256 // initiate pipes to pass data to/from incident_helper
257 if (!p2cPipe.init() || !c2pPipe.init()) {
258 ALOGW("FileSection '%s' failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700259 return -errno;
260 }
261
Yi Jin1a11fa12018-02-22 16:44:10 -0800262 pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700263 if (pid == -1) {
264 ALOGW("FileSection '%s' failed to fork", this->name.string());
265 return -errno;
266 }
267
268 // parent process
269 status_t readStatus = buffer.readProcessedDataInStream(fd, p2cPipe.writeFd(), c2pPipe.readFd(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800270 this->timeoutMs, mIsSysfs);
Yi Jin1a11fa12018-02-22 16:44:10 -0800271 close(fd); // close the fd anyway.
272
Yi Jinb44f7d42017-07-21 12:12:59 -0700273 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin4bab3a12018-01-10 16:50:59 -0800274 ALOGW("FileSection '%s' failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800275 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800276 kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700277 return readStatus;
278 }
279
Yi Jinedfd5bb2017-09-06 17:09:11 -0700280 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700281 if (ihStatus != NO_ERROR) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800282 ALOGW("FileSection '%s' abnormal child process: %s", this->name.string(),
283 strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700284 return ihStatus;
285 }
286
Yi Jinb592e3b2018-02-01 15:17:04 -0800287 VLOG("FileSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
288 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700289 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jin0a3406f2017-06-22 19:23:11 -0700290 if (err != NO_ERROR) {
291 ALOGW("FileSection '%s' failed writing: %s", this->name.string(), strerror(-err));
292 return err;
293 }
294
295 return NO_ERROR;
296}
Yi Jin1a11fa12018-02-22 16:44:10 -0800297// ================================================================================
298GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) {
299 name = "gzip ";
300 name += filename;
301 va_list args;
302 va_start(args, filename);
303 mFilenames = varargs(filename, args);
304 va_end(args);
305}
Yi Jin0a3406f2017-06-22 19:23:11 -0700306
Yi Jin1a11fa12018-02-22 16:44:10 -0800307GZipSection::~GZipSection() {}
308
309status_t GZipSection::Execute(ReportRequestSet* requests) const {
310 // Reads the files in order, use the first available one.
311 int index = 0;
312 int fd = -1;
313 while (mFilenames[index] != NULL) {
314 fd = open(mFilenames[index], O_RDONLY | O_CLOEXEC);
315 if (fd != -1) {
316 break;
317 }
318 ALOGW("GZipSection failed to open file %s", mFilenames[index]);
319 index++; // look at the next file.
320 }
321 VLOG("GZipSection is using file %s, fd=%d", mFilenames[index], fd);
322 if (fd == -1) return -1;
323
324 FdBuffer buffer;
325 Fpipe p2cPipe;
326 Fpipe c2pPipe;
327 // initiate pipes to pass data to/from gzip
328 if (!p2cPipe.init() || !c2pPipe.init()) {
329 ALOGW("GZipSection '%s' failed to setup pipes", this->name.string());
330 return -errno;
331 }
332
333 const char* gzipArgs[]{GZIP, NULL};
334 pid_t pid = fork_execute_cmd(GZIP, const_cast<char**>(gzipArgs), &p2cPipe, &c2pPipe);
335 if (pid == -1) {
336 ALOGW("GZipSection '%s' failed to fork", this->name.string());
337 return -errno;
338 }
339 // parent process
340
341 // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using
342 // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream.
343 EncodedBuffer* internalBuffer = buffer.getInternalBuffer();
344 internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED);
345 String8 usedFile(mFilenames[index]);
346 internalBuffer->writeRawVarint32(usedFile.size());
347 for (size_t i = 0; i < usedFile.size(); i++) {
348 internalBuffer->writeRawByte(mFilenames[index][i]);
349 }
350 internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA,
351 WIRE_TYPE_LENGTH_DELIMITED);
352 size_t editPos = internalBuffer->wp()->pos();
353 internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size.
354 size_t dataBeginAt = internalBuffer->wp()->pos();
355 VLOG("GZipSection '%s' editPos=%zd, dataBeginAt=%zd", this->name.string(), editPos,
356 dataBeginAt);
357
358 status_t readStatus = buffer.readProcessedDataInStream(
359 fd, p2cPipe.writeFd(), c2pPipe.readFd(), this->timeoutMs, isSysfs(mFilenames[index]));
360 close(fd); // close the fd anyway.
361
362 if (readStatus != NO_ERROR || buffer.timedOut()) {
363 ALOGW("GZipSection '%s' failed to read data from gzip: %s, timedout: %s",
364 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
365 kill_child(pid);
366 return readStatus;
367 }
368
369 status_t gzipStatus = wait_child(pid);
370 if (gzipStatus != NO_ERROR) {
371 ALOGW("GZipSection '%s' abnormal child process: %s", this->name.string(),
372 strerror(-gzipStatus));
373 return gzipStatus;
374 }
375 // Revisit the actual size from gzip result and edit the internal buffer accordingly.
376 size_t dataSize = buffer.size() - dataBeginAt;
377 internalBuffer->wp()->rewind()->move(editPos);
378 internalBuffer->writeRawVarint32(dataSize);
379 internalBuffer->copy(dataBeginAt, dataSize);
380 VLOG("GZipSection '%s' wrote %zd bytes in %d ms, dataSize=%zd", this->name.string(),
381 buffer.size(), (int)buffer.durationMs(), dataSize);
382 status_t err = write_report_requests(this->id, buffer, requests);
383 if (err != NO_ERROR) {
384 ALOGW("GZipSection '%s' failed writing: %s", this->name.string(), strerror(-err));
385 return err;
386 }
387
388 return NO_ERROR;
389}
Yi Jin0a3406f2017-06-22 19:23:11 -0700390// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800391struct WorkerThreadData : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800392 const WorkerThreadSection* section;
393 int fds[2];
394
395 // Lock protects these fields
396 mutex lock;
397 bool workerDone;
398 status_t workerError;
399
400 WorkerThreadData(const WorkerThreadSection* section);
401 virtual ~WorkerThreadData();
402
403 int readFd() { return fds[0]; }
404 int writeFd() { return fds[1]; }
405};
406
407WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
Yi Jinb592e3b2018-02-01 15:17:04 -0800408 : section(sec), workerDone(false), workerError(NO_ERROR) {
Joe Onorato1754d742016-11-21 17:51:35 -0800409 fds[0] = -1;
410 fds[1] = -1;
411}
412
Yi Jinb592e3b2018-02-01 15:17:04 -0800413WorkerThreadData::~WorkerThreadData() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800414
415// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800416WorkerThreadSection::WorkerThreadSection(int id) : Section(id) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800417
Yi Jinb592e3b2018-02-01 15:17:04 -0800418WorkerThreadSection::~WorkerThreadSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800419
Yi Jinb592e3b2018-02-01 15:17:04 -0800420static void* worker_thread_func(void* cookie) {
Joe Onorato1754d742016-11-21 17:51:35 -0800421 WorkerThreadData* data = (WorkerThreadData*)cookie;
422 status_t err = data->section->BlockingCall(data->writeFd());
423
424 {
425 unique_lock<mutex> lock(data->lock);
426 data->workerDone = true;
427 data->workerError = err;
428 }
429
430 close(data->writeFd());
431 data->decStrong(data->section);
432 // data might be gone now. don't use it after this point in this thread.
433 return NULL;
434}
435
Yi Jinb592e3b2018-02-01 15:17:04 -0800436status_t WorkerThreadSection::Execute(ReportRequestSet* requests) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800437 status_t err = NO_ERROR;
438 pthread_t thread;
439 pthread_attr_t attr;
440 bool timedOut = false;
441 FdBuffer buffer;
442
443 // Data shared between this thread and the worker thread.
444 sp<WorkerThreadData> data = new WorkerThreadData(this);
445
446 // Create the pipe
447 err = pipe(data->fds);
448 if (err != 0) {
449 return -errno;
450 }
451
452 // The worker thread needs a reference and we can't let the count go to zero
453 // if that thread is slow to start.
454 data->incStrong(this);
455
456 // Create the thread
457 err = pthread_attr_init(&attr);
458 if (err != 0) {
459 return -err;
460 }
461 // TODO: Do we need to tweak thread priority?
462 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
463 if (err != 0) {
464 pthread_attr_destroy(&attr);
465 return -err;
466 }
467 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
468 if (err != 0) {
469 pthread_attr_destroy(&attr);
470 return -err;
471 }
472 pthread_attr_destroy(&attr);
473
474 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jinb44f7d42017-07-21 12:12:59 -0700475 err = buffer.read(data->readFd(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800476 if (err != NO_ERROR) {
477 // TODO: Log this error into the incident report.
478 ALOGW("WorkerThreadSection '%s' reader failed with error '%s'", this->name.string(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800479 strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800480 }
481
482 // Done with the read fd. The worker thread closes the write one so
483 // we never race and get here first.
484 close(data->readFd());
485
486 // If the worker side is finished, then return its error (which may overwrite
487 // our possible error -- but it's more interesting anyway). If not, then we timed out.
488 {
489 unique_lock<mutex> lock(data->lock);
490 if (!data->workerDone) {
491 // We timed out
492 timedOut = true;
493 } else {
494 if (data->workerError != NO_ERROR) {
495 err = data->workerError;
496 // TODO: Log this error into the incident report.
497 ALOGW("WorkerThreadSection '%s' worker failed with error '%s'", this->name.string(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800498 strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800499 }
500 }
501 }
502
503 if (timedOut || buffer.timedOut()) {
504 ALOGW("WorkerThreadSection '%s' timed out", this->name.string());
505 return NO_ERROR;
506 }
507
508 if (buffer.truncated()) {
509 // TODO: Log this into the incident report.
510 }
511
512 // TODO: There was an error with the command or buffering. Report that. For now
513 // just exit with a log messasge.
514 if (err != NO_ERROR) {
515 ALOGW("WorkerThreadSection '%s' failed with error '%s'", this->name.string(),
Yi Jinb592e3b2018-02-01 15:17:04 -0800516 strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800517 return NO_ERROR;
518 }
519
520 // Write the data that was collected
Yi Jinb592e3b2018-02-01 15:17:04 -0800521 VLOG("WorkerThreadSection '%s' wrote %zd bytes in %d ms", name.string(), buffer.size(),
522 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700523 err = write_report_requests(this->id, buffer, requests);
Joe Onorato1754d742016-11-21 17:51:35 -0800524 if (err != NO_ERROR) {
525 ALOGW("WorkerThreadSection '%s' failed writing: '%s'", this->name.string(), strerror(-err));
526 return err;
527 }
528
529 return NO_ERROR;
530}
531
532// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700533CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800534 : Section(id, timeoutMs) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800535 name = command;
Joe Onorato1754d742016-11-21 17:51:35 -0800536 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700537 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800538 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800539 va_end(args);
Yi Jinb44f7d42017-07-21 12:12:59 -0700540}
Joe Onorato1754d742016-11-21 17:51:35 -0800541
Yi Jinb592e3b2018-02-01 15:17:04 -0800542CommandSection::CommandSection(int id, const char* command, ...) : Section(id) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800543 name = command;
Yi Jinb44f7d42017-07-21 12:12:59 -0700544 va_list args;
545 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800546 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800547 va_end(args);
548}
549
Yi Jinb592e3b2018-02-01 15:17:04 -0800550CommandSection::~CommandSection() { free(mCommand); }
Joe Onorato1754d742016-11-21 17:51:35 -0800551
Yi Jinb592e3b2018-02-01 15:17:04 -0800552status_t CommandSection::Execute(ReportRequestSet* requests) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700553 FdBuffer buffer;
554 Fpipe cmdPipe;
555 Fpipe ihPipe;
556
557 if (!cmdPipe.init() || !ihPipe.init()) {
558 ALOGW("CommandSection '%s' failed to setup pipes", this->name.string());
559 return -errno;
560 }
561
562 pid_t cmdPid = fork();
563 if (cmdPid == -1) {
564 ALOGW("CommandSection '%s' failed to fork", this->name.string());
565 return -errno;
566 }
567 // child process to execute the command as root
568 if (cmdPid == 0) {
569 // replace command's stdout with ihPipe's write Fd
570 if (dup2(cmdPipe.writeFd(), STDOUT_FILENO) != 1 || !ihPipe.close() || !cmdPipe.close()) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800571 ALOGW("CommandSection '%s' failed to set up stdout: %s", this->name.string(),
572 strerror(errno));
Yi Jinb44f7d42017-07-21 12:12:59 -0700573 _exit(EXIT_FAILURE);
574 }
Yi Jinb592e3b2018-02-01 15:17:04 -0800575 execvp(this->mCommand[0], (char* const*)this->mCommand);
576 int err = errno; // record command error code
577 ALOGW("CommandSection '%s' failed in executing command: %s", this->name.string(),
578 strerror(errno));
579 _exit(err); // exit with command error code
Yi Jinb44f7d42017-07-21 12:12:59 -0700580 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800581 pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700582 if (ihPid == -1) {
583 ALOGW("CommandSection '%s' failed to fork", this->name.string());
584 return -errno;
585 }
586
587 close(cmdPipe.writeFd());
588 status_t readStatus = buffer.read(ihPipe.readFd(), this->timeoutMs);
589 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin4bab3a12018-01-10 16:50:59 -0800590 ALOGW("CommandSection '%s' failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800591 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800592 kill_child(cmdPid);
593 kill_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700594 return readStatus;
595 }
596
Yi Jinb592e3b2018-02-01 15:17:04 -0800597 // TODO: wait for command here has one trade-off: the failed status of command won't be detected
Yi Jin1a11fa12018-02-22 16:44:10 -0800598 // until buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700599 status_t cmdStatus = wait_child(cmdPid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800600 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700601 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800602 ALOGW("CommandSection '%s' abnormal child processes, return status: command: %s, incident "
603 "helper: %s",
604 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700605 return cmdStatus != NO_ERROR ? cmdStatus : ihStatus;
606 }
607
Yi Jinb592e3b2018-02-01 15:17:04 -0800608 VLOG("CommandSection '%s' wrote %zd bytes in %d ms", this->name.string(), buffer.size(),
609 (int)buffer.durationMs());
Yi Jinedfd5bb2017-09-06 17:09:11 -0700610 status_t err = write_report_requests(this->id, buffer, requests);
Yi Jinb44f7d42017-07-21 12:12:59 -0700611 if (err != NO_ERROR) {
612 ALOGW("CommandSection '%s' failed writing: %s", this->name.string(), strerror(-err));
613 return err;
614 }
Joe Onorato1754d742016-11-21 17:51:35 -0800615 return NO_ERROR;
616}
617
618// ================================================================================
619DumpsysSection::DumpsysSection(int id, const char* service, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800620 : WorkerThreadSection(id), mService(service) {
Joe Onorato1754d742016-11-21 17:51:35 -0800621 name = "dumpsys ";
622 name += service;
623
624 va_list args;
625 va_start(args, service);
626 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700627 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800628 if (arg == NULL) {
629 break;
630 }
631 mArgs.add(String16(arg));
632 name += " ";
633 name += arg;
634 }
635 va_end(args);
636}
637
Yi Jinb592e3b2018-02-01 15:17:04 -0800638DumpsysSection::~DumpsysSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800639
Yi Jinb592e3b2018-02-01 15:17:04 -0800640status_t DumpsysSection::BlockingCall(int pipeWriteFd) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800641 // checkService won't wait for the service to show up like getService will.
642 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700643
Joe Onorato1754d742016-11-21 17:51:35 -0800644 if (service == NULL) {
645 // Returning an error interrupts the entire incident report, so just
646 // log the failure.
647 // TODO: have a meta record inside the report that would log this
648 // failure inside the report, because the fact that we can't find
649 // the service is good data in and of itself. This is running in
650 // another thread so lock that carefully...
651 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
652 return NO_ERROR;
653 }
654
655 service->dump(pipeWriteFd, mArgs);
656
657 return NO_ERROR;
658}
Yi Jin3c034c92017-12-22 17:36:47 -0800659
660// ================================================================================
661// initialization only once in Section.cpp.
662map<log_id_t, log_time> LogSection::gLastLogsRetrieved;
663
Yi Jinb592e3b2018-02-01 15:17:04 -0800664LogSection::LogSection(int id, log_id_t logID) : WorkerThreadSection(id), mLogID(logID) {
Yi Jin3c034c92017-12-22 17:36:47 -0800665 name += "logcat ";
666 name += android_log_id_to_name(logID);
667 switch (logID) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800668 case LOG_ID_EVENTS:
669 case LOG_ID_STATS:
670 case LOG_ID_SECURITY:
671 mBinary = true;
672 break;
673 default:
674 mBinary = false;
Yi Jin3c034c92017-12-22 17:36:47 -0800675 }
676}
677
Yi Jinb592e3b2018-02-01 15:17:04 -0800678LogSection::~LogSection() {}
Yi Jin3c034c92017-12-22 17:36:47 -0800679
Yi Jinb592e3b2018-02-01 15:17:04 -0800680static size_t trimTail(char const* buf, size_t len) {
Yi Jin3c034c92017-12-22 17:36:47 -0800681 while (len > 0) {
682 char c = buf[len - 1];
683 if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') {
684 len--;
685 } else {
686 break;
687 }
688 }
689 return len;
690}
691
692static inline int32_t get4LE(uint8_t const* src) {
693 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
694}
695
Yi Jinb592e3b2018-02-01 15:17:04 -0800696status_t LogSection::BlockingCall(int pipeWriteFd) const {
Yi Jin3c034c92017-12-22 17:36:47 -0800697 status_t err = NO_ERROR;
698 // Open log buffer and getting logs since last retrieved time if any.
699 unique_ptr<logger_list, void (*)(logger_list*)> loggers(
Yi Jinb592e3b2018-02-01 15:17:04 -0800700 gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end()
701 ? android_logger_list_alloc(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0)
702 : android_logger_list_alloc_time(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK,
703 gLastLogsRetrieved[mLogID], 0),
704 android_logger_list_free);
Yi Jin3c034c92017-12-22 17:36:47 -0800705
706 if (android_logger_open(loggers.get(), mLogID) == NULL) {
707 ALOGW("LogSection %s: Can't get logger.", this->name.string());
708 return err;
709 }
710
711 log_msg msg;
712 log_time lastTimestamp(0);
713
714 ProtoOutputStream proto;
Yi Jinb592e3b2018-02-01 15:17:04 -0800715 while (true) { // keeps reading until logd buffer is fully read.
Yi Jin3c034c92017-12-22 17:36:47 -0800716 status_t err = android_logger_list_read(loggers.get(), &msg);
717 // err = 0 - no content, unexpected connection drop or EOF.
718 // err = +ive number - size of retrieved data from logger
719 // err = -ive number, OS supplied error _except_ for -EAGAIN
720 // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data.
721 if (err <= 0) {
722 if (err != -EAGAIN) {
723 ALOGE("LogSection %s: fails to read a log_msg.\n", this->name.string());
724 }
725 break;
726 }
727 if (mBinary) {
728 // remove the first uint32 which is tag's index in event log tags
729 android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t),
Yi Jinb592e3b2018-02-01 15:17:04 -0800730 msg.len() - sizeof(uint32_t));
731 ;
Yi Jin3c034c92017-12-22 17:36:47 -0800732 android_log_list_element elem;
733
734 lastTimestamp.tv_sec = msg.entry_v1.sec;
735 lastTimestamp.tv_nsec = msg.entry_v1.nsec;
736
737 // format a BinaryLogEntry
738 long long token = proto.start(LogProto::BINARY_LOGS);
739 proto.write(BinaryLogEntry::SEC, msg.entry_v1.sec);
740 proto.write(BinaryLogEntry::NANOSEC, msg.entry_v1.nsec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800741 proto.write(BinaryLogEntry::UID, (int)msg.entry_v4.uid);
Yi Jin3c034c92017-12-22 17:36:47 -0800742 proto.write(BinaryLogEntry::PID, msg.entry_v1.pid);
743 proto.write(BinaryLogEntry::TID, msg.entry_v1.tid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800744 proto.write(BinaryLogEntry::TAG_INDEX,
745 get4LE(reinterpret_cast<uint8_t const*>(msg.msg())));
Yi Jin3c034c92017-12-22 17:36:47 -0800746 do {
747 elem = android_log_read_next(context);
748 long long elemToken = proto.start(BinaryLogEntry::ELEMS);
749 switch (elem.type) {
750 case EVENT_TYPE_INT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800751 proto.write(BinaryLogEntry::Elem::TYPE,
752 BinaryLogEntry::Elem::EVENT_TYPE_INT);
753 proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32);
Yi Jin3c034c92017-12-22 17:36:47 -0800754 break;
755 case EVENT_TYPE_LONG:
Yi Jinb592e3b2018-02-01 15:17:04 -0800756 proto.write(BinaryLogEntry::Elem::TYPE,
757 BinaryLogEntry::Elem::EVENT_TYPE_LONG);
758 proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64);
Yi Jin3c034c92017-12-22 17:36:47 -0800759 break;
760 case EVENT_TYPE_STRING:
Yi Jinb592e3b2018-02-01 15:17:04 -0800761 proto.write(BinaryLogEntry::Elem::TYPE,
762 BinaryLogEntry::Elem::EVENT_TYPE_STRING);
Yi Jin3c034c92017-12-22 17:36:47 -0800763 proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len);
764 break;
765 case EVENT_TYPE_FLOAT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800766 proto.write(BinaryLogEntry::Elem::TYPE,
767 BinaryLogEntry::Elem::EVENT_TYPE_FLOAT);
Yi Jin3c034c92017-12-22 17:36:47 -0800768 proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32);
769 break;
770 case EVENT_TYPE_LIST:
Yi Jinb592e3b2018-02-01 15:17:04 -0800771 proto.write(BinaryLogEntry::Elem::TYPE,
772 BinaryLogEntry::Elem::EVENT_TYPE_LIST);
Yi Jin3c034c92017-12-22 17:36:47 -0800773 break;
774 case EVENT_TYPE_LIST_STOP:
Yi Jinb592e3b2018-02-01 15:17:04 -0800775 proto.write(BinaryLogEntry::Elem::TYPE,
776 BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP);
Yi Jin3c034c92017-12-22 17:36:47 -0800777 break;
778 case EVENT_TYPE_UNKNOWN:
Yi Jinb592e3b2018-02-01 15:17:04 -0800779 proto.write(BinaryLogEntry::Elem::TYPE,
780 BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN);
Yi Jin3c034c92017-12-22 17:36:47 -0800781 break;
782 }
783 proto.end(elemToken);
784 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
785 proto.end(token);
786 if (context) {
787 android_log_destroy(&context);
788 }
789 } else {
790 AndroidLogEntry entry;
791 err = android_log_processLogBuffer(&msg.entry_v1, &entry);
792 if (err != NO_ERROR) {
793 ALOGE("LogSection %s: fails to process to an entry.\n", this->name.string());
794 break;
795 }
796 lastTimestamp.tv_sec = entry.tv_sec;
797 lastTimestamp.tv_nsec = entry.tv_nsec;
798
799 // format a TextLogEntry
800 long long token = proto.start(LogProto::TEXT_LOGS);
801 proto.write(TextLogEntry::SEC, (long long)entry.tv_sec);
802 proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec);
803 proto.write(TextLogEntry::PRIORITY, (int)entry.priority);
804 proto.write(TextLogEntry::UID, entry.uid);
805 proto.write(TextLogEntry::PID, entry.pid);
806 proto.write(TextLogEntry::TID, entry.tid);
807 proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen));
Yi Jinb592e3b2018-02-01 15:17:04 -0800808 proto.write(TextLogEntry::LOG, entry.message,
809 trimTail(entry.message, entry.messageLen));
Yi Jin3c034c92017-12-22 17:36:47 -0800810 proto.end(token);
811 }
812 }
813 gLastLogsRetrieved[mLogID] = lastTimestamp;
814 proto.flush(pipeWriteFd);
815 return err;
816}