blob: 1e8261ee1832e87d3c82e9bdb903110a2fbcb29b [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
Kweku Adamseadd1232018-02-05 16:45:13 -080021#include <dirent.h>
22#include <errno.h>
Yi Jin3c034c92017-12-22 17:36:47 -080023
Yi Jin3c034c92017-12-22 17:36:47 -080024#include <mutex>
Kweku Adamseadd1232018-02-05 16:45:13 -080025#include <set>
Joe Onorato1754d742016-11-21 17:51:35 -080026
Yi Jinb592e3b2018-02-01 15:17:04 -080027#include <android-base/file.h>
Kweku Adamseadd1232018-02-05 16:45:13 -080028#include <android-base/stringprintf.h>
Yi Jinc23fad22017-09-15 17:24:59 -070029#include <android/util/protobuf.h>
Joe Onorato99598ee2019-02-11 15:55:13 +000030#include <android/util/ProtoOutputStream.h>
Joe Onorato1754d742016-11-21 17:51:35 -080031#include <binder/IServiceManager.h>
Kweku Adamseadd1232018-02-05 16:45:13 -080032#include <debuggerd/client.h>
33#include <dumputils/dump_utils.h>
Yi Jin3c034c92017-12-22 17:36:47 -080034#include <log/log_event_list.h>
Yi Jin3c034c92017-12-22 17:36:47 -080035#include <log/log_read.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080036#include <log/logprint.h>
Yi Jin3c034c92017-12-22 17:36:47 -080037#include <private/android_logger.h>
38
39#include "FdBuffer.h"
Yi Jin3c034c92017-12-22 17:36:47 -080040#include "Privacy.h"
Kweku Adamseadd1232018-02-05 16:45:13 -080041#include "frameworks/base/core/proto/android/os/backtrace.proto.h"
Yi Jin1a11fa12018-02-22 16:44:10 -080042#include "frameworks/base/core/proto/android/os/data.proto.h"
Yi Jinb592e3b2018-02-01 15:17:04 -080043#include "frameworks/base/core/proto/android/util/log.proto.h"
44#include "incidentd_util.h"
Joe Onorato1754d742016-11-21 17:51:35 -080045
Yi Jin6cacbcb2018-03-30 14:04:52 -070046namespace android {
47namespace os {
48namespace incidentd {
49
Yi Jinb592e3b2018-02-01 15:17:04 -080050using namespace android::base;
Yi Jinc23fad22017-09-15 17:24:59 -070051using namespace android::util;
Joe Onorato1754d742016-11-21 17:51:35 -080052
Yi Jinc23fad22017-09-15 17:24:59 -070053// special section ids
Yi Jin329130b2018-02-09 16:47:47 -080054const int FIELD_ID_INCIDENT_METADATA = 2;
Yi Jinc23fad22017-09-15 17:24:59 -070055
56// incident section parameters
Yi Jin3c034c92017-12-22 17:36:47 -080057const char INCIDENT_HELPER[] = "/system/bin/incident_helper";
Yi Jinc36e91d2018-03-08 11:25:58 -080058const char* GZIP[] = {"/system/bin/gzip", NULL};
Yi Jinb44f7d42017-07-21 12:12:59 -070059
Yi Jin1a11fa12018-02-22 16:44:10 -080060static pid_t fork_execute_incident_helper(const int id, Fpipe* p2cPipe, Fpipe* c2pPipe) {
Yi Jinb592e3b2018-02-01 15:17:04 -080061 const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL};
Yi Jinc36e91d2018-03-08 11:25:58 -080062 return fork_execute_cmd(const_cast<char**>(ihArgs), p2cPipe, c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -070063}
64
Yi Jin99c248f2017-08-25 18:11:58 -070065// ================================================================================
Kweku Adamse04ef772018-06-13 12:24:38 -070066Section::Section(int i, int64_t timeoutMs, bool userdebugAndEngOnly)
Kweku Adams3d160912018-05-07 11:26:27 -070067 : id(i),
68 timeoutMs(timeoutMs),
Kweku Adamse04ef772018-06-13 12:24:38 -070069 userdebugAndEngOnly(userdebugAndEngOnly) {}
Joe Onorato1754d742016-11-21 17:51:35 -080070
Yi Jinb592e3b2018-02-01 15:17:04 -080071Section::~Section() {}
Joe Onorato1754d742016-11-21 17:51:35 -080072
Joe Onorato1754d742016-11-21 17:51:35 -080073// ================================================================================
Yi Jin1a11fa12018-02-22 16:44:10 -080074static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; }
75
Kweku Adamse04ef772018-06-13 12:24:38 -070076FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
77 : Section(id, timeoutMs, false), mFilename(filename) {
Yi Jin3be0b432018-04-20 17:08:11 -070078 name = "file ";
79 name += filename;
Yi Jin1a11fa12018-02-22 16:44:10 -080080 mIsSysfs = isSysfs(filename);
Yi Jin0a3406f2017-06-22 19:23:11 -070081}
82
83FileSection::~FileSection() {}
84
Joe Onorato99598ee2019-02-11 15:55:13 +000085status_t FileSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -070086 // read from mFilename first, make sure the file is available
87 // add O_CLOEXEC to make sure it is closed when exec incident helper
Yi Jin6355d2f2018-03-14 15:18:02 -070088 unique_fd fd(open(mFilename, O_RDONLY | O_CLOEXEC));
89 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -070090 ALOGW("[%s] failed to open file", this->name.string());
Kweku Adamse04ef772018-06-13 12:24:38 -070091 // There may be some devices/architectures that won't have the file.
92 // Just return here without an error.
93 return NO_ERROR;
Yi Jin0a3406f2017-06-22 19:23:11 -070094 }
95
Yi Jinb44f7d42017-07-21 12:12:59 -070096 FdBuffer buffer;
97 Fpipe p2cPipe;
98 Fpipe c2pPipe;
99 // initiate pipes to pass data to/from incident_helper
100 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700101 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700102 return -errno;
103 }
104
Yi Jin1a11fa12018-02-22 16:44:10 -0800105 pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700106 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700107 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700108 return -errno;
109 }
110
111 // parent process
Yi Jine3dab2d2018-03-22 16:56:39 -0700112 status_t readStatus = buffer.readProcessedDataInStream(fd.get(), std::move(p2cPipe.writeFd()),
113 std::move(c2pPipe.readFd()),
114 this->timeoutMs, mIsSysfs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000115 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700116 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700117 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800118 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800119 kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700120 return readStatus;
121 }
122
Yi Jinedfd5bb2017-09-06 17:09:11 -0700123 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700124 if (ihStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700125 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700126 return ihStatus;
127 }
128
Joe Onorato99598ee2019-02-11 15:55:13 +0000129 return writer->writeSection(buffer);
Yi Jin0a3406f2017-06-22 19:23:11 -0700130}
Yi Jin1a11fa12018-02-22 16:44:10 -0800131// ================================================================================
132GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800133 va_list args;
134 va_start(args, filename);
135 mFilenames = varargs(filename, args);
136 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800137 name = "gzip";
138 for (int i = 0; mFilenames[i] != NULL; i++) {
139 name += " ";
140 name += mFilenames[i];
141 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800142}
Yi Jin0a3406f2017-06-22 19:23:11 -0700143
Yi Jin480de782018-04-06 15:37:36 -0700144GZipSection::~GZipSection() { free(mFilenames); }
Yi Jin1a11fa12018-02-22 16:44:10 -0800145
Joe Onorato99598ee2019-02-11 15:55:13 +0000146status_t GZipSection::Execute(ReportWriter* writer) const {
Yi Jin1a11fa12018-02-22 16:44:10 -0800147 // Reads the files in order, use the first available one.
148 int index = 0;
Yi Jin6355d2f2018-03-14 15:18:02 -0700149 unique_fd fd;
Yi Jin1a11fa12018-02-22 16:44:10 -0800150 while (mFilenames[index] != NULL) {
Yi Jin6355d2f2018-03-14 15:18:02 -0700151 fd.reset(open(mFilenames[index], O_RDONLY | O_CLOEXEC));
152 if (fd.get() != -1) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800153 break;
154 }
155 ALOGW("GZipSection failed to open file %s", mFilenames[index]);
156 index++; // look at the next file.
157 }
Yi Jinc858e272018-03-28 15:32:50 -0700158 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700159 ALOGW("[%s] can't open all the files", this->name.string());
Yi Jin6cacbcb2018-03-30 14:04:52 -0700160 return NO_ERROR; // e.g. LAST_KMSG will reach here in user build.
Yi Jinc858e272018-03-28 15:32:50 -0700161 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800162 FdBuffer buffer;
163 Fpipe p2cPipe;
164 Fpipe c2pPipe;
165 // initiate pipes to pass data to/from gzip
166 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700167 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800168 return -errno;
169 }
170
Yi Jinc36e91d2018-03-08 11:25:58 -0800171 pid_t pid = fork_execute_cmd((char* const*)GZIP, &p2cPipe, &c2pPipe);
Yi Jin1a11fa12018-02-22 16:44:10 -0800172 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700173 ALOGW("[%s] failed to fork", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800174 return -errno;
175 }
176 // parent process
177
178 // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using
179 // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream.
Joe Onorato99598ee2019-02-11 15:55:13 +0000180 sp<EncodedBuffer> internalBuffer = buffer.data();
Yi Jin1a11fa12018-02-22 16:44:10 -0800181 internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED);
Yi Jina9635e42018-03-23 12:05:32 -0700182 size_t fileLen = strlen(mFilenames[index]);
183 internalBuffer->writeRawVarint32(fileLen);
184 for (size_t i = 0; i < fileLen; i++) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800185 internalBuffer->writeRawByte(mFilenames[index][i]);
186 }
187 internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA,
188 WIRE_TYPE_LENGTH_DELIMITED);
189 size_t editPos = internalBuffer->wp()->pos();
190 internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size.
191 size_t dataBeginAt = internalBuffer->wp()->pos();
Yi Jin3be0b432018-04-20 17:08:11 -0700192 VLOG("[%s] editPos=%zu, dataBeginAt=%zu", this->name.string(), editPos, dataBeginAt);
Yi Jin1a11fa12018-02-22 16:44:10 -0800193
Yi Jine3dab2d2018-03-22 16:56:39 -0700194 status_t readStatus = buffer.readProcessedDataInStream(
195 fd.get(), std::move(p2cPipe.writeFd()), std::move(c2pPipe.readFd()), this->timeoutMs,
196 isSysfs(mFilenames[index]));
Joe Onorato99598ee2019-02-11 15:55:13 +0000197 writer->setSectionStats(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800198 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700199 ALOGW("[%s] failed to read data from gzip: %s, timedout: %s", this->name.string(),
200 strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin1a11fa12018-02-22 16:44:10 -0800201 kill_child(pid);
202 return readStatus;
203 }
204
205 status_t gzipStatus = wait_child(pid);
206 if (gzipStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700207 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-gzipStatus));
Yi Jin1a11fa12018-02-22 16:44:10 -0800208 return gzipStatus;
209 }
210 // Revisit the actual size from gzip result and edit the internal buffer accordingly.
211 size_t dataSize = buffer.size() - dataBeginAt;
212 internalBuffer->wp()->rewind()->move(editPos);
213 internalBuffer->writeRawVarint32(dataSize);
214 internalBuffer->copy(dataBeginAt, dataSize);
Yi Jin1a11fa12018-02-22 16:44:10 -0800215
Joe Onorato99598ee2019-02-11 15:55:13 +0000216 return writer->writeSection(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800217}
Kweku Adamseadd1232018-02-05 16:45:13 -0800218
Yi Jin0a3406f2017-06-22 19:23:11 -0700219// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800220struct WorkerThreadData : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800221 const WorkerThreadSection* section;
Yi Jin6355d2f2018-03-14 15:18:02 -0700222 Fpipe pipe;
Joe Onorato1754d742016-11-21 17:51:35 -0800223
224 // Lock protects these fields
225 mutex lock;
226 bool workerDone;
227 status_t workerError;
228
Chih-Hung Hsieh7a88a932018-12-20 13:45:04 -0800229 explicit WorkerThreadData(const WorkerThreadSection* section);
Joe Onorato1754d742016-11-21 17:51:35 -0800230 virtual ~WorkerThreadData();
Joe Onorato1754d742016-11-21 17:51:35 -0800231};
232
233WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
Yi Jin6355d2f2018-03-14 15:18:02 -0700234 : section(sec), workerDone(false), workerError(NO_ERROR) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800235
Yi Jinb592e3b2018-02-01 15:17:04 -0800236WorkerThreadData::~WorkerThreadData() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800237
238// ================================================================================
Kweku Adams3d160912018-05-07 11:26:27 -0700239WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs, bool userdebugAndEngOnly)
240 : Section(id, timeoutMs, userdebugAndEngOnly) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800241
Yi Jinb592e3b2018-02-01 15:17:04 -0800242WorkerThreadSection::~WorkerThreadSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800243
Kweku Adams5b763c12018-09-13 15:44:58 -0700244void sigpipe_handler(int signum) {
245 if (signum == SIGPIPE) {
246 ALOGE("Wrote to a broken pipe\n");
247 } else {
248 ALOGE("Received unexpected signal: %d\n", signum);
249 }
250}
251
Yi Jinb592e3b2018-02-01 15:17:04 -0800252static void* worker_thread_func(void* cookie) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700253 // Don't crash the service if we write to a closed pipe (which can happen if
254 // dumping times out).
255 signal(SIGPIPE, sigpipe_handler);
256
Joe Onorato1754d742016-11-21 17:51:35 -0800257 WorkerThreadData* data = (WorkerThreadData*)cookie;
Yi Jin6355d2f2018-03-14 15:18:02 -0700258 status_t err = data->section->BlockingCall(data->pipe.writeFd().get());
Joe Onorato1754d742016-11-21 17:51:35 -0800259
260 {
261 unique_lock<mutex> lock(data->lock);
262 data->workerDone = true;
263 data->workerError = err;
264 }
265
Yi Jin6355d2f2018-03-14 15:18:02 -0700266 data->pipe.writeFd().reset();
Joe Onorato1754d742016-11-21 17:51:35 -0800267 data->decStrong(data->section);
268 // data might be gone now. don't use it after this point in this thread.
269 return NULL;
270}
271
Joe Onorato99598ee2019-02-11 15:55:13 +0000272status_t WorkerThreadSection::Execute(ReportWriter* writer) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800273 status_t err = NO_ERROR;
274 pthread_t thread;
275 pthread_attr_t attr;
Mike Ma28381692018-12-04 15:46:29 -0800276 bool workerDone = false;
Joe Onorato1754d742016-11-21 17:51:35 -0800277 FdBuffer buffer;
278
279 // Data shared between this thread and the worker thread.
280 sp<WorkerThreadData> data = new WorkerThreadData(this);
281
282 // Create the pipe
Yi Jin6355d2f2018-03-14 15:18:02 -0700283 if (!data->pipe.init()) {
Joe Onorato1754d742016-11-21 17:51:35 -0800284 return -errno;
285 }
286
Joe Onorato1754d742016-11-21 17:51:35 -0800287 // Create the thread
288 err = pthread_attr_init(&attr);
289 if (err != 0) {
290 return -err;
291 }
292 // TODO: Do we need to tweak thread priority?
293 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
294 if (err != 0) {
295 pthread_attr_destroy(&attr);
296 return -err;
297 }
Joe Onorato99598ee2019-02-11 15:55:13 +0000298
299 // The worker thread needs a reference and we can't let the count go to zero
300 // if that thread is slow to start.
301 data->incStrong(this);
302
Joe Onorato1754d742016-11-21 17:51:35 -0800303 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
Joe Onorato99598ee2019-02-11 15:55:13 +0000304 pthread_attr_destroy(&attr);
Joe Onorato1754d742016-11-21 17:51:35 -0800305 if (err != 0) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000306 data->decStrong(this);
Joe Onorato1754d742016-11-21 17:51:35 -0800307 return -err;
308 }
Joe Onorato1754d742016-11-21 17:51:35 -0800309
310 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jine3dab2d2018-03-22 16:56:39 -0700311 err = buffer.read(data->pipe.readFd().get(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800312 if (err != NO_ERROR) {
Mike Ma28381692018-12-04 15:46:29 -0800313 ALOGE("[%s] reader failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800314 }
315
316 // Done with the read fd. The worker thread closes the write one so
317 // we never race and get here first.
Yi Jin6355d2f2018-03-14 15:18:02 -0700318 data->pipe.readFd().reset();
Joe Onorato1754d742016-11-21 17:51:35 -0800319
320 // If the worker side is finished, then return its error (which may overwrite
Mike Ma28381692018-12-04 15:46:29 -0800321 // our possible error -- but it's more interesting anyway). If not, then we timed out.
Joe Onorato1754d742016-11-21 17:51:35 -0800322 {
323 unique_lock<mutex> lock(data->lock);
Mike Ma28381692018-12-04 15:46:29 -0800324 if (data->workerError != NO_ERROR) {
325 err = data->workerError;
326 ALOGE("[%s] worker failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800327 }
Mike Ma28381692018-12-04 15:46:29 -0800328 workerDone = data->workerDone;
Joe Onorato1754d742016-11-21 17:51:35 -0800329 }
Kweku Adams5b763c12018-09-13 15:44:58 -0700330
Joe Onorato99598ee2019-02-11 15:55:13 +0000331 writer->setSectionStats(buffer);
Mike Ma28381692018-12-04 15:46:29 -0800332 if (err != NO_ERROR) {
333 char errMsg[128];
334 snprintf(errMsg, 128, "[%s] failed with error '%s'",
335 this->name.string(), strerror(-err));
Joe Onorato99598ee2019-02-11 15:55:13 +0000336 writer->error(this, err, "WorkerThreadSection failed.");
Joe Onorato1754d742016-11-21 17:51:35 -0800337 return NO_ERROR;
338 }
Mike Ma28381692018-12-04 15:46:29 -0800339 if (buffer.truncated()) {
340 ALOGW("[%s] too large, truncating", this->name.string());
Joe Onorato99598ee2019-02-11 15:55:13 +0000341 // Do not write a truncated section. It won't pass through the PrivacyFilter.
Mike Ma28381692018-12-04 15:46:29 -0800342 return NO_ERROR;
343 }
344 if (!workerDone || buffer.timedOut()) {
345 ALOGW("[%s] timed out", this->name.string());
Joe Onorato1754d742016-11-21 17:51:35 -0800346 return NO_ERROR;
347 }
348
349 // Write the data that was collected
Joe Onorato99598ee2019-02-11 15:55:13 +0000350 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800351}
352
353// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700354CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800355 : Section(id, timeoutMs) {
Joe Onorato1754d742016-11-21 17:51:35 -0800356 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700357 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800358 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800359 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800360 name = "cmd";
361 for (int i = 0; mCommand[i] != NULL; i++) {
362 name += " ";
363 name += mCommand[i];
364 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700365}
Joe Onorato1754d742016-11-21 17:51:35 -0800366
Yi Jinb592e3b2018-02-01 15:17:04 -0800367CommandSection::CommandSection(int id, const char* command, ...) : Section(id) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700368 va_list args;
369 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800370 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800371 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800372 name = "cmd";
373 for (int i = 0; mCommand[i] != NULL; i++) {
374 name += " ";
375 name += mCommand[i];
376 }
Joe Onorato1754d742016-11-21 17:51:35 -0800377}
378
Yi Jinb592e3b2018-02-01 15:17:04 -0800379CommandSection::~CommandSection() { free(mCommand); }
Joe Onorato1754d742016-11-21 17:51:35 -0800380
Joe Onorato99598ee2019-02-11 15:55:13 +0000381status_t CommandSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700382 FdBuffer buffer;
383 Fpipe cmdPipe;
384 Fpipe ihPipe;
385
386 if (!cmdPipe.init() || !ihPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700387 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700388 return -errno;
389 }
390
Yi Jinc36e91d2018-03-08 11:25:58 -0800391 pid_t cmdPid = fork_execute_cmd((char* const*)mCommand, NULL, &cmdPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700392 if (cmdPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700393 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700394 return -errno;
395 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800396 pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700397 if (ihPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700398 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700399 return -errno;
400 }
401
Yi Jin6355d2f2018-03-14 15:18:02 -0700402 cmdPipe.writeFd().reset();
Yi Jine3dab2d2018-03-22 16:56:39 -0700403 status_t readStatus = buffer.read(ihPipe.readFd().get(), this->timeoutMs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000404 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700405 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700406 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800407 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800408 kill_child(cmdPid);
409 kill_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700410 return readStatus;
411 }
412
Kweku Adamseadd1232018-02-05 16:45:13 -0800413 // Waiting for command here has one trade-off: the failed status of command won't be detected
Yi Jin1a11fa12018-02-22 16:44:10 -0800414 // until buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700415 status_t cmdStatus = wait_child(cmdPid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800416 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700417 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000418 ALOGW("[%s] abnormal child processes, return status: command: %s, incident helper: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800419 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
Joe Onorato99598ee2019-02-11 15:55:13 +0000420 // Not a fatal error.
421 return NO_ERROR;
Yi Jinb44f7d42017-07-21 12:12:59 -0700422 }
423
Joe Onorato99598ee2019-02-11 15:55:13 +0000424 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800425}
426
427// ================================================================================
Kweku Adams3d160912018-05-07 11:26:27 -0700428DumpsysSection::DumpsysSection(int id, bool userdebugAndEngOnly, const char* service, ...)
429 : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS, userdebugAndEngOnly), mService(service) {
Joe Onorato1754d742016-11-21 17:51:35 -0800430 name = "dumpsys ";
431 name += service;
432
433 va_list args;
434 va_start(args, service);
435 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700436 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800437 if (arg == NULL) {
438 break;
439 }
440 mArgs.add(String16(arg));
441 name += " ";
442 name += arg;
443 }
444 va_end(args);
445}
446
Yi Jinb592e3b2018-02-01 15:17:04 -0800447DumpsysSection::~DumpsysSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800448
Yi Jinb592e3b2018-02-01 15:17:04 -0800449status_t DumpsysSection::BlockingCall(int pipeWriteFd) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800450 // checkService won't wait for the service to show up like getService will.
451 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700452
Joe Onorato1754d742016-11-21 17:51:35 -0800453 if (service == NULL) {
Joe Onorato1754d742016-11-21 17:51:35 -0800454 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
Mike Ma28381692018-12-04 15:46:29 -0800455 return NAME_NOT_FOUND;
Joe Onorato1754d742016-11-21 17:51:35 -0800456 }
457
458 service->dump(pipeWriteFd, mArgs);
459
460 return NO_ERROR;
461}
Yi Jin3c034c92017-12-22 17:36:47 -0800462
463// ================================================================================
464// initialization only once in Section.cpp.
465map<log_id_t, log_time> LogSection::gLastLogsRetrieved;
466
Yi Jinb592e3b2018-02-01 15:17:04 -0800467LogSection::LogSection(int id, log_id_t logID) : WorkerThreadSection(id), mLogID(logID) {
Yi Jin3be0b432018-04-20 17:08:11 -0700468 name = "logcat ";
Yi Jin3c034c92017-12-22 17:36:47 -0800469 name += android_log_id_to_name(logID);
470 switch (logID) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800471 case LOG_ID_EVENTS:
472 case LOG_ID_STATS:
473 case LOG_ID_SECURITY:
474 mBinary = true;
475 break;
476 default:
477 mBinary = false;
Yi Jin3c034c92017-12-22 17:36:47 -0800478 }
479}
480
Yi Jinb592e3b2018-02-01 15:17:04 -0800481LogSection::~LogSection() {}
Yi Jin3c034c92017-12-22 17:36:47 -0800482
Yi Jinb592e3b2018-02-01 15:17:04 -0800483static size_t trimTail(char const* buf, size_t len) {
Yi Jin3c034c92017-12-22 17:36:47 -0800484 while (len > 0) {
485 char c = buf[len - 1];
486 if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') {
487 len--;
488 } else {
489 break;
490 }
491 }
492 return len;
493}
494
495static inline int32_t get4LE(uint8_t const* src) {
496 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
497}
498
Yi Jinb592e3b2018-02-01 15:17:04 -0800499status_t LogSection::BlockingCall(int pipeWriteFd) const {
Yi Jin3c034c92017-12-22 17:36:47 -0800500 // Open log buffer and getting logs since last retrieved time if any.
501 unique_ptr<logger_list, void (*)(logger_list*)> loggers(
Yi Jinb592e3b2018-02-01 15:17:04 -0800502 gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end()
503 ? android_logger_list_alloc(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0)
504 : android_logger_list_alloc_time(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK,
505 gLastLogsRetrieved[mLogID], 0),
506 android_logger_list_free);
Yi Jin3c034c92017-12-22 17:36:47 -0800507
508 if (android_logger_open(loggers.get(), mLogID) == NULL) {
Yi Jin3be0b432018-04-20 17:08:11 -0700509 ALOGE("[%s] Can't get logger.", this->name.string());
Yi Jin83fb1d52018-03-16 12:03:53 -0700510 return -1;
Yi Jin3c034c92017-12-22 17:36:47 -0800511 }
512
513 log_msg msg;
514 log_time lastTimestamp(0);
515
516 ProtoOutputStream proto;
Yi Jinb592e3b2018-02-01 15:17:04 -0800517 while (true) { // keeps reading until logd buffer is fully read.
Yi Jin83fb1d52018-03-16 12:03:53 -0700518 status_t err = android_logger_list_read(loggers.get(), &msg);
Yi Jin3c034c92017-12-22 17:36:47 -0800519 // err = 0 - no content, unexpected connection drop or EOF.
520 // err = +ive number - size of retrieved data from logger
521 // err = -ive number, OS supplied error _except_ for -EAGAIN
522 // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data.
523 if (err <= 0) {
524 if (err != -EAGAIN) {
Yi Jin3be0b432018-04-20 17:08:11 -0700525 ALOGW("[%s] fails to read a log_msg.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800526 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700527 // dump previous logs and don't consider this error a failure.
Yi Jin3c034c92017-12-22 17:36:47 -0800528 break;
529 }
530 if (mBinary) {
531 // remove the first uint32 which is tag's index in event log tags
532 android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t),
Yi Jinb592e3b2018-02-01 15:17:04 -0800533 msg.len() - sizeof(uint32_t));
534 ;
Yi Jin3c034c92017-12-22 17:36:47 -0800535 android_log_list_element elem;
536
537 lastTimestamp.tv_sec = msg.entry_v1.sec;
538 lastTimestamp.tv_nsec = msg.entry_v1.nsec;
539
540 // format a BinaryLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800541 uint64_t token = proto.start(LogProto::BINARY_LOGS);
Yi Jin3c034c92017-12-22 17:36:47 -0800542 proto.write(BinaryLogEntry::SEC, msg.entry_v1.sec);
543 proto.write(BinaryLogEntry::NANOSEC, msg.entry_v1.nsec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800544 proto.write(BinaryLogEntry::UID, (int)msg.entry_v4.uid);
Yi Jin3c034c92017-12-22 17:36:47 -0800545 proto.write(BinaryLogEntry::PID, msg.entry_v1.pid);
546 proto.write(BinaryLogEntry::TID, msg.entry_v1.tid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800547 proto.write(BinaryLogEntry::TAG_INDEX,
548 get4LE(reinterpret_cast<uint8_t const*>(msg.msg())));
Yi Jin3c034c92017-12-22 17:36:47 -0800549 do {
550 elem = android_log_read_next(context);
Yi Jin5ee07872018-03-05 18:18:27 -0800551 uint64_t elemToken = proto.start(BinaryLogEntry::ELEMS);
Yi Jin3c034c92017-12-22 17:36:47 -0800552 switch (elem.type) {
553 case EVENT_TYPE_INT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800554 proto.write(BinaryLogEntry::Elem::TYPE,
555 BinaryLogEntry::Elem::EVENT_TYPE_INT);
556 proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32);
Yi Jin3c034c92017-12-22 17:36:47 -0800557 break;
558 case EVENT_TYPE_LONG:
Yi Jinb592e3b2018-02-01 15:17:04 -0800559 proto.write(BinaryLogEntry::Elem::TYPE,
560 BinaryLogEntry::Elem::EVENT_TYPE_LONG);
561 proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64);
Yi Jin3c034c92017-12-22 17:36:47 -0800562 break;
563 case EVENT_TYPE_STRING:
Yi Jinb592e3b2018-02-01 15:17:04 -0800564 proto.write(BinaryLogEntry::Elem::TYPE,
565 BinaryLogEntry::Elem::EVENT_TYPE_STRING);
Yi Jin3c034c92017-12-22 17:36:47 -0800566 proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len);
567 break;
568 case EVENT_TYPE_FLOAT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800569 proto.write(BinaryLogEntry::Elem::TYPE,
570 BinaryLogEntry::Elem::EVENT_TYPE_FLOAT);
Yi Jin3c034c92017-12-22 17:36:47 -0800571 proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32);
572 break;
573 case EVENT_TYPE_LIST:
Yi Jinb592e3b2018-02-01 15:17:04 -0800574 proto.write(BinaryLogEntry::Elem::TYPE,
575 BinaryLogEntry::Elem::EVENT_TYPE_LIST);
Yi Jin3c034c92017-12-22 17:36:47 -0800576 break;
577 case EVENT_TYPE_LIST_STOP:
Yi Jinb592e3b2018-02-01 15:17:04 -0800578 proto.write(BinaryLogEntry::Elem::TYPE,
579 BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP);
Yi Jin3c034c92017-12-22 17:36:47 -0800580 break;
581 case EVENT_TYPE_UNKNOWN:
Yi Jinb592e3b2018-02-01 15:17:04 -0800582 proto.write(BinaryLogEntry::Elem::TYPE,
583 BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN);
Yi Jin3c034c92017-12-22 17:36:47 -0800584 break;
585 }
586 proto.end(elemToken);
587 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
588 proto.end(token);
589 if (context) {
590 android_log_destroy(&context);
591 }
592 } else {
593 AndroidLogEntry entry;
594 err = android_log_processLogBuffer(&msg.entry_v1, &entry);
595 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700596 ALOGW("[%s] fails to process to an entry.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800597 break;
598 }
599 lastTimestamp.tv_sec = entry.tv_sec;
600 lastTimestamp.tv_nsec = entry.tv_nsec;
601
602 // format a TextLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800603 uint64_t token = proto.start(LogProto::TEXT_LOGS);
Yi Jin3c034c92017-12-22 17:36:47 -0800604 proto.write(TextLogEntry::SEC, (long long)entry.tv_sec);
605 proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec);
606 proto.write(TextLogEntry::PRIORITY, (int)entry.priority);
607 proto.write(TextLogEntry::UID, entry.uid);
608 proto.write(TextLogEntry::PID, entry.pid);
609 proto.write(TextLogEntry::TID, entry.tid);
610 proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen));
Yi Jinb592e3b2018-02-01 15:17:04 -0800611 proto.write(TextLogEntry::LOG, entry.message,
612 trimTail(entry.message, entry.messageLen));
Yi Jin3c034c92017-12-22 17:36:47 -0800613 proto.end(token);
614 }
615 }
616 gLastLogsRetrieved[mLogID] = lastTimestamp;
Kweku Adams5b763c12018-09-13 15:44:58 -0700617 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
618 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
619 return EPIPE;
620 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700621 return NO_ERROR;
Yi Jin3c034c92017-12-22 17:36:47 -0800622}
Kweku Adamseadd1232018-02-05 16:45:13 -0800623
624// ================================================================================
625
626TombstoneSection::TombstoneSection(int id, const char* type, const int64_t timeoutMs)
627 : WorkerThreadSection(id, timeoutMs), mType(type) {
Yi Jin3be0b432018-04-20 17:08:11 -0700628 name = "tombstone ";
Kweku Adamseadd1232018-02-05 16:45:13 -0800629 name += type;
630}
631
632TombstoneSection::~TombstoneSection() {}
633
634status_t TombstoneSection::BlockingCall(int pipeWriteFd) const {
635 std::unique_ptr<DIR, decltype(&closedir)> proc(opendir("/proc"), closedir);
636 if (proc.get() == nullptr) {
637 ALOGE("opendir /proc failed: %s\n", strerror(errno));
638 return -errno;
639 }
640
641 const std::set<int> hal_pids = get_interesting_hal_pids();
642
643 ProtoOutputStream proto;
644 struct dirent* d;
645 status_t err = NO_ERROR;
646 while ((d = readdir(proc.get()))) {
647 int pid = atoi(d->d_name);
648 if (pid <= 0) {
649 continue;
650 }
651
652 const std::string link_name = android::base::StringPrintf("/proc/%d/exe", pid);
653 std::string exe;
654 if (!android::base::Readlink(link_name, &exe)) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000655 ALOGE("Section %s: Can't read '%s': %s\n", name.string(),
656 link_name.c_str(), strerror(errno));
Kweku Adamseadd1232018-02-05 16:45:13 -0800657 continue;
658 }
659
660 bool is_java_process;
661 if (exe == "/system/bin/app_process32" || exe == "/system/bin/app_process64") {
662 if (mType != "java") continue;
663 // Don't bother dumping backtraces for the zygote.
664 if (IsZygote(pid)) {
665 VLOG("Skipping Zygote");
666 continue;
667 }
668
669 is_java_process = true;
670 } else if (should_dump_native_traces(exe.c_str())) {
671 if (mType != "native") continue;
672 is_java_process = false;
673 } else if (hal_pids.find(pid) != hal_pids.end()) {
674 if (mType != "hal") continue;
675 is_java_process = false;
676 } else {
677 // Probably a native process we don't care about, continue.
678 VLOG("Skipping %d", pid);
679 continue;
680 }
681
682 Fpipe dumpPipe;
683 if (!dumpPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700684 ALOGW("[%s] failed to setup dump pipe", this->name.string());
Kweku Adamseadd1232018-02-05 16:45:13 -0800685 err = -errno;
686 break;
687 }
688
689 const uint64_t start = Nanotime();
690 pid_t child = fork();
691 if (child < 0) {
692 ALOGE("Failed to fork child process");
693 break;
694 } else if (child == 0) {
695 // This is the child process.
Yi Jin6355d2f2018-03-14 15:18:02 -0700696 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800697 const int ret = dump_backtrace_to_file_timeout(
698 pid, is_java_process ? kDebuggerdJavaBacktrace : kDebuggerdNativeBacktrace,
Yi Jin6355d2f2018-03-14 15:18:02 -0700699 is_java_process ? 5 : 20, dumpPipe.writeFd().get());
Kweku Adamseadd1232018-02-05 16:45:13 -0800700 if (ret == -1) {
701 if (errno == 0) {
702 ALOGW("Dumping failed for pid '%d', likely due to a timeout\n", pid);
703 } else {
704 ALOGE("Dumping failed for pid '%d': %s\n", pid, strerror(errno));
705 }
706 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700707 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800708 _exit(EXIT_SUCCESS);
709 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700710 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800711 // Parent process.
712 // Read from the pipe concurrently to avoid blocking the child.
713 FdBuffer buffer;
Yi Jine3dab2d2018-03-22 16:56:39 -0700714 err = buffer.readFully(dumpPipe.readFd().get());
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700715 // Wait on the child to avoid it becoming a zombie process.
716 status_t cStatus = wait_child(child);
Kweku Adamseadd1232018-02-05 16:45:13 -0800717 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700718 ALOGW("[%s] failed to read stack dump: %d", this->name.string(), err);
Yi Jin6355d2f2018-03-14 15:18:02 -0700719 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800720 break;
721 }
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700722 if (cStatus != NO_ERROR) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700723 ALOGE("[%s] child had an issue: %s\n", this->name.string(), strerror(-cStatus));
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700724 }
Kweku Adamseadd1232018-02-05 16:45:13 -0800725
726 auto dump = std::make_unique<char[]>(buffer.size());
Joe Onorato99598ee2019-02-11 15:55:13 +0000727 sp<ProtoReader> reader = buffer.data()->read();
Kweku Adamseadd1232018-02-05 16:45:13 -0800728 int i = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000729 while (reader->hasNext()) {
730 dump[i] = reader->next();
Kweku Adamseadd1232018-02-05 16:45:13 -0800731 i++;
732 }
Yi Jin6cacbcb2018-03-30 14:04:52 -0700733 uint64_t token = proto.start(android::os::BackTraceProto::TRACES);
Kweku Adamseadd1232018-02-05 16:45:13 -0800734 proto.write(android::os::BackTraceProto::Stack::PID, pid);
735 proto.write(android::os::BackTraceProto::Stack::DUMP, dump.get(), i);
736 proto.write(android::os::BackTraceProto::Stack::DUMP_DURATION_NS,
737 static_cast<long long>(Nanotime() - start));
738 proto.end(token);
Yi Jin6355d2f2018-03-14 15:18:02 -0700739 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800740 }
741
Kweku Adams5b763c12018-09-13 15:44:58 -0700742 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
743 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
744 if (err != NO_ERROR) {
745 return EPIPE;
746 }
747 }
748
Kweku Adamseadd1232018-02-05 16:45:13 -0800749 return err;
750}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700751
752} // namespace incidentd
753} // namespace os
Yi Jin98ce8102018-04-12 11:15:39 -0700754} // namespace android