blob: 935a7c43fe900a849c8fdc635ad51d3efc599d15 [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>
Joe Onoratofe7bbf42019-03-24 20:57:16 -070028#include <android-base/properties.h>
Kweku Adamseadd1232018-02-05 16:45:13 -080029#include <android-base/stringprintf.h>
Yi Jinc23fad22017-09-15 17:24:59 -070030#include <android/util/protobuf.h>
Joe Onorato99598ee2019-02-11 15:55:13 +000031#include <android/util/ProtoOutputStream.h>
Joe Onorato1754d742016-11-21 17:51:35 -080032#include <binder/IServiceManager.h>
Kweku Adamseadd1232018-02-05 16:45:13 -080033#include <debuggerd/client.h>
34#include <dumputils/dump_utils.h>
Yi Jin3c034c92017-12-22 17:36:47 -080035#include <log/log_event_list.h>
Yi Jin3c034c92017-12-22 17:36:47 -080036#include <log/log_read.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080037#include <log/logprint.h>
Yi Jin3c034c92017-12-22 17:36:47 -080038#include <private/android_logger.h>
39
40#include "FdBuffer.h"
Yi Jin3c034c92017-12-22 17:36:47 -080041#include "Privacy.h"
Kweku Adamseadd1232018-02-05 16:45:13 -080042#include "frameworks/base/core/proto/android/os/backtrace.proto.h"
Yi Jin1a11fa12018-02-22 16:44:10 -080043#include "frameworks/base/core/proto/android/os/data.proto.h"
Yi Jinb592e3b2018-02-01 15:17:04 -080044#include "frameworks/base/core/proto/android/util/log.proto.h"
45#include "incidentd_util.h"
Joe Onorato1754d742016-11-21 17:51:35 -080046
Yi Jin6cacbcb2018-03-30 14:04:52 -070047namespace android {
48namespace os {
49namespace incidentd {
50
Yi Jinb592e3b2018-02-01 15:17:04 -080051using namespace android::base;
Yi Jinc23fad22017-09-15 17:24:59 -070052using namespace android::util;
Joe Onorato1754d742016-11-21 17:51:35 -080053
Yi Jinc23fad22017-09-15 17:24:59 -070054// special section ids
Yi Jin329130b2018-02-09 16:47:47 -080055const int FIELD_ID_INCIDENT_METADATA = 2;
Yi Jinc23fad22017-09-15 17:24:59 -070056
57// incident section parameters
Yi Jin3c034c92017-12-22 17:36:47 -080058const char INCIDENT_HELPER[] = "/system/bin/incident_helper";
Yi Jinc36e91d2018-03-08 11:25:58 -080059const char* GZIP[] = {"/system/bin/gzip", NULL};
Yi Jinb44f7d42017-07-21 12:12:59 -070060
Yi Jin1a11fa12018-02-22 16:44:10 -080061static pid_t fork_execute_incident_helper(const int id, Fpipe* p2cPipe, Fpipe* c2pPipe) {
Yi Jinb592e3b2018-02-01 15:17:04 -080062 const char* ihArgs[]{INCIDENT_HELPER, "-s", String8::format("%d", id).string(), NULL};
Yi Jinc36e91d2018-03-08 11:25:58 -080063 return fork_execute_cmd(const_cast<char**>(ihArgs), p2cPipe, c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -070064}
65
Yi Jin99c248f2017-08-25 18:11:58 -070066// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -070067Section::Section(int i, int64_t timeoutMs)
Kweku Adams3d160912018-05-07 11:26:27 -070068 : id(i),
Joe Onoratofe7bbf42019-03-24 20:57:16 -070069 timeoutMs(timeoutMs) {
70}
Joe Onorato1754d742016-11-21 17:51:35 -080071
Yi Jinb592e3b2018-02-01 15:17:04 -080072Section::~Section() {}
Joe Onorato1754d742016-11-21 17:51:35 -080073
Joe Onorato1754d742016-11-21 17:51:35 -080074// ================================================================================
Yi Jin1a11fa12018-02-22 16:44:10 -080075static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; }
76
Kweku Adamse04ef772018-06-13 12:24:38 -070077FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Joe Onoratofe7bbf42019-03-24 20:57:16 -070078 : Section(id, timeoutMs), mFilename(filename) {
Yi Jin3be0b432018-04-20 17:08:11 -070079 name = "file ";
80 name += filename;
Yi Jin1a11fa12018-02-22 16:44:10 -080081 mIsSysfs = isSysfs(filename);
Yi Jin0a3406f2017-06-22 19:23:11 -070082}
83
84FileSection::~FileSection() {}
85
Joe Onorato99598ee2019-02-11 15:55:13 +000086status_t FileSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -070087 // read from mFilename first, make sure the file is available
88 // add O_CLOEXEC to make sure it is closed when exec incident helper
Yi Jin6355d2f2018-03-14 15:18:02 -070089 unique_fd fd(open(mFilename, O_RDONLY | O_CLOEXEC));
90 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -070091 ALOGW("[%s] failed to open file", this->name.string());
Kweku Adamse04ef772018-06-13 12:24:38 -070092 // There may be some devices/architectures that won't have the file.
93 // Just return here without an error.
94 return NO_ERROR;
Yi Jin0a3406f2017-06-22 19:23:11 -070095 }
96
Yi Jinb44f7d42017-07-21 12:12:59 -070097 FdBuffer buffer;
98 Fpipe p2cPipe;
99 Fpipe c2pPipe;
100 // initiate pipes to pass data to/from incident_helper
101 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700102 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700103 return -errno;
104 }
105
Yi Jin1a11fa12018-02-22 16:44:10 -0800106 pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700107 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700108 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700109 return -errno;
110 }
111
112 // parent process
Yi Jine3dab2d2018-03-22 16:56:39 -0700113 status_t readStatus = buffer.readProcessedDataInStream(fd.get(), std::move(p2cPipe.writeFd()),
114 std::move(c2pPipe.readFd()),
115 this->timeoutMs, mIsSysfs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000116 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700117 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700118 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800119 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800120 kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700121 return readStatus;
122 }
123
Yi Jinedfd5bb2017-09-06 17:09:11 -0700124 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700125 if (ihStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700126 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700127 return ihStatus;
128 }
129
Joe Onorato99598ee2019-02-11 15:55:13 +0000130 return writer->writeSection(buffer);
Yi Jin0a3406f2017-06-22 19:23:11 -0700131}
Yi Jin1a11fa12018-02-22 16:44:10 -0800132// ================================================================================
133GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800134 va_list args;
135 va_start(args, filename);
136 mFilenames = varargs(filename, args);
137 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800138 name = "gzip";
139 for (int i = 0; mFilenames[i] != NULL; i++) {
140 name += " ";
141 name += mFilenames[i];
142 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800143}
Yi Jin0a3406f2017-06-22 19:23:11 -0700144
Yi Jin480de782018-04-06 15:37:36 -0700145GZipSection::~GZipSection() { free(mFilenames); }
Yi Jin1a11fa12018-02-22 16:44:10 -0800146
Joe Onorato99598ee2019-02-11 15:55:13 +0000147status_t GZipSection::Execute(ReportWriter* writer) const {
Yi Jin1a11fa12018-02-22 16:44:10 -0800148 // Reads the files in order, use the first available one.
149 int index = 0;
Yi Jin6355d2f2018-03-14 15:18:02 -0700150 unique_fd fd;
Yi Jin1a11fa12018-02-22 16:44:10 -0800151 while (mFilenames[index] != NULL) {
Yi Jin6355d2f2018-03-14 15:18:02 -0700152 fd.reset(open(mFilenames[index], O_RDONLY | O_CLOEXEC));
153 if (fd.get() != -1) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800154 break;
155 }
156 ALOGW("GZipSection failed to open file %s", mFilenames[index]);
157 index++; // look at the next file.
158 }
Yi Jinc858e272018-03-28 15:32:50 -0700159 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700160 ALOGW("[%s] can't open all the files", this->name.string());
Yi Jin6cacbcb2018-03-30 14:04:52 -0700161 return NO_ERROR; // e.g. LAST_KMSG will reach here in user build.
Yi Jinc858e272018-03-28 15:32:50 -0700162 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800163 FdBuffer buffer;
164 Fpipe p2cPipe;
165 Fpipe c2pPipe;
166 // initiate pipes to pass data to/from gzip
167 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700168 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800169 return -errno;
170 }
171
Yi Jinc36e91d2018-03-08 11:25:58 -0800172 pid_t pid = fork_execute_cmd((char* const*)GZIP, &p2cPipe, &c2pPipe);
Yi Jin1a11fa12018-02-22 16:44:10 -0800173 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700174 ALOGW("[%s] failed to fork", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800175 return -errno;
176 }
177 // parent process
178
179 // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using
180 // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream.
Joe Onorato99598ee2019-02-11 15:55:13 +0000181 sp<EncodedBuffer> internalBuffer = buffer.data();
Yi Jin1a11fa12018-02-22 16:44:10 -0800182 internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED);
Yi Jina9635e42018-03-23 12:05:32 -0700183 size_t fileLen = strlen(mFilenames[index]);
184 internalBuffer->writeRawVarint32(fileLen);
185 for (size_t i = 0; i < fileLen; i++) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800186 internalBuffer->writeRawByte(mFilenames[index][i]);
187 }
188 internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA,
189 WIRE_TYPE_LENGTH_DELIMITED);
190 size_t editPos = internalBuffer->wp()->pos();
191 internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size.
192 size_t dataBeginAt = internalBuffer->wp()->pos();
Yi Jin3be0b432018-04-20 17:08:11 -0700193 VLOG("[%s] editPos=%zu, dataBeginAt=%zu", this->name.string(), editPos, dataBeginAt);
Yi Jin1a11fa12018-02-22 16:44:10 -0800194
Yi Jine3dab2d2018-03-22 16:56:39 -0700195 status_t readStatus = buffer.readProcessedDataInStream(
196 fd.get(), std::move(p2cPipe.writeFd()), std::move(c2pPipe.readFd()), this->timeoutMs,
197 isSysfs(mFilenames[index]));
Joe Onorato99598ee2019-02-11 15:55:13 +0000198 writer->setSectionStats(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800199 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700200 ALOGW("[%s] failed to read data from gzip: %s, timedout: %s", this->name.string(),
201 strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin1a11fa12018-02-22 16:44:10 -0800202 kill_child(pid);
203 return readStatus;
204 }
205
206 status_t gzipStatus = wait_child(pid);
207 if (gzipStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700208 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-gzipStatus));
Yi Jin1a11fa12018-02-22 16:44:10 -0800209 return gzipStatus;
210 }
211 // Revisit the actual size from gzip result and edit the internal buffer accordingly.
212 size_t dataSize = buffer.size() - dataBeginAt;
213 internalBuffer->wp()->rewind()->move(editPos);
214 internalBuffer->writeRawVarint32(dataSize);
215 internalBuffer->copy(dataBeginAt, dataSize);
Yi Jin1a11fa12018-02-22 16:44:10 -0800216
Joe Onorato99598ee2019-02-11 15:55:13 +0000217 return writer->writeSection(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800218}
Kweku Adamseadd1232018-02-05 16:45:13 -0800219
Yi Jin0a3406f2017-06-22 19:23:11 -0700220// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800221struct WorkerThreadData : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800222 const WorkerThreadSection* section;
Yi Jin6355d2f2018-03-14 15:18:02 -0700223 Fpipe pipe;
Joe Onorato1754d742016-11-21 17:51:35 -0800224
225 // Lock protects these fields
226 mutex lock;
227 bool workerDone;
228 status_t workerError;
229
Chih-Hung Hsieh7a88a932018-12-20 13:45:04 -0800230 explicit WorkerThreadData(const WorkerThreadSection* section);
Joe Onorato1754d742016-11-21 17:51:35 -0800231 virtual ~WorkerThreadData();
Joe Onorato1754d742016-11-21 17:51:35 -0800232};
233
234WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
Yi Jin6355d2f2018-03-14 15:18:02 -0700235 : section(sec), workerDone(false), workerError(NO_ERROR) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800236
Yi Jinb592e3b2018-02-01 15:17:04 -0800237WorkerThreadData::~WorkerThreadData() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800238
239// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700240WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs)
241 : Section(id, timeoutMs) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800242
Yi Jinb592e3b2018-02-01 15:17:04 -0800243WorkerThreadSection::~WorkerThreadSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800244
Kweku Adams5b763c12018-09-13 15:44:58 -0700245void sigpipe_handler(int signum) {
246 if (signum == SIGPIPE) {
247 ALOGE("Wrote to a broken pipe\n");
248 } else {
249 ALOGE("Received unexpected signal: %d\n", signum);
250 }
251}
252
Yi Jinb592e3b2018-02-01 15:17:04 -0800253static void* worker_thread_func(void* cookie) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700254 // Don't crash the service if we write to a closed pipe (which can happen if
255 // dumping times out).
256 signal(SIGPIPE, sigpipe_handler);
257
Joe Onorato1754d742016-11-21 17:51:35 -0800258 WorkerThreadData* data = (WorkerThreadData*)cookie;
Yi Jin6355d2f2018-03-14 15:18:02 -0700259 status_t err = data->section->BlockingCall(data->pipe.writeFd().get());
Joe Onorato1754d742016-11-21 17:51:35 -0800260
261 {
262 unique_lock<mutex> lock(data->lock);
263 data->workerDone = true;
264 data->workerError = err;
265 }
266
Yi Jin6355d2f2018-03-14 15:18:02 -0700267 data->pipe.writeFd().reset();
Joe Onorato1754d742016-11-21 17:51:35 -0800268 data->decStrong(data->section);
269 // data might be gone now. don't use it after this point in this thread.
270 return NULL;
271}
272
Joe Onorato99598ee2019-02-11 15:55:13 +0000273status_t WorkerThreadSection::Execute(ReportWriter* writer) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800274 status_t err = NO_ERROR;
275 pthread_t thread;
276 pthread_attr_t attr;
Mike Ma28381692018-12-04 15:46:29 -0800277 bool workerDone = false;
Joe Onorato1754d742016-11-21 17:51:35 -0800278 FdBuffer buffer;
279
280 // Data shared between this thread and the worker thread.
281 sp<WorkerThreadData> data = new WorkerThreadData(this);
282
283 // Create the pipe
Yi Jin6355d2f2018-03-14 15:18:02 -0700284 if (!data->pipe.init()) {
Joe Onorato1754d742016-11-21 17:51:35 -0800285 return -errno;
286 }
287
Joe Onorato1754d742016-11-21 17:51:35 -0800288 // Create the thread
289 err = pthread_attr_init(&attr);
290 if (err != 0) {
291 return -err;
292 }
293 // TODO: Do we need to tweak thread priority?
294 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
295 if (err != 0) {
296 pthread_attr_destroy(&attr);
297 return -err;
298 }
Joe Onorato99598ee2019-02-11 15:55:13 +0000299
300 // The worker thread needs a reference and we can't let the count go to zero
301 // if that thread is slow to start.
302 data->incStrong(this);
303
Joe Onorato1754d742016-11-21 17:51:35 -0800304 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
Joe Onorato99598ee2019-02-11 15:55:13 +0000305 pthread_attr_destroy(&attr);
Joe Onorato1754d742016-11-21 17:51:35 -0800306 if (err != 0) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000307 data->decStrong(this);
Joe Onorato1754d742016-11-21 17:51:35 -0800308 return -err;
309 }
Joe Onorato1754d742016-11-21 17:51:35 -0800310
311 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jine3dab2d2018-03-22 16:56:39 -0700312 err = buffer.read(data->pipe.readFd().get(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800313 if (err != NO_ERROR) {
Mike Ma28381692018-12-04 15:46:29 -0800314 ALOGE("[%s] reader failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800315 }
316
317 // Done with the read fd. The worker thread closes the write one so
318 // we never race and get here first.
Yi Jin6355d2f2018-03-14 15:18:02 -0700319 data->pipe.readFd().reset();
Joe Onorato1754d742016-11-21 17:51:35 -0800320
321 // If the worker side is finished, then return its error (which may overwrite
Mike Ma28381692018-12-04 15:46:29 -0800322 // our possible error -- but it's more interesting anyway). If not, then we timed out.
Joe Onorato1754d742016-11-21 17:51:35 -0800323 {
324 unique_lock<mutex> lock(data->lock);
Mike Ma28381692018-12-04 15:46:29 -0800325 if (data->workerError != NO_ERROR) {
326 err = data->workerError;
327 ALOGE("[%s] worker failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800328 }
Mike Ma28381692018-12-04 15:46:29 -0800329 workerDone = data->workerDone;
Joe Onorato1754d742016-11-21 17:51:35 -0800330 }
Kweku Adams5b763c12018-09-13 15:44:58 -0700331
Joe Onorato99598ee2019-02-11 15:55:13 +0000332 writer->setSectionStats(buffer);
Mike Ma28381692018-12-04 15:46:29 -0800333 if (err != NO_ERROR) {
334 char errMsg[128];
335 snprintf(errMsg, 128, "[%s] failed with error '%s'",
336 this->name.string(), strerror(-err));
Joe Onorato99598ee2019-02-11 15:55:13 +0000337 writer->error(this, err, "WorkerThreadSection failed.");
Joe Onorato1754d742016-11-21 17:51:35 -0800338 return NO_ERROR;
339 }
Mike Ma28381692018-12-04 15:46:29 -0800340 if (buffer.truncated()) {
341 ALOGW("[%s] too large, truncating", this->name.string());
Joe Onorato99598ee2019-02-11 15:55:13 +0000342 // Do not write a truncated section. It won't pass through the PrivacyFilter.
Mike Ma28381692018-12-04 15:46:29 -0800343 return NO_ERROR;
344 }
345 if (!workerDone || buffer.timedOut()) {
346 ALOGW("[%s] timed out", this->name.string());
Joe Onorato1754d742016-11-21 17:51:35 -0800347 return NO_ERROR;
348 }
349
350 // Write the data that was collected
Joe Onorato99598ee2019-02-11 15:55:13 +0000351 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800352}
353
354// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700355CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800356 : Section(id, timeoutMs) {
Joe Onorato1754d742016-11-21 17:51:35 -0800357 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700358 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800359 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800360 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800361 name = "cmd";
362 for (int i = 0; mCommand[i] != NULL; i++) {
363 name += " ";
364 name += mCommand[i];
365 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700366}
Joe Onorato1754d742016-11-21 17:51:35 -0800367
Yi Jinb592e3b2018-02-01 15:17:04 -0800368CommandSection::CommandSection(int id, const char* command, ...) : Section(id) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700369 va_list args;
370 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800371 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800372 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800373 name = "cmd";
374 for (int i = 0; mCommand[i] != NULL; i++) {
375 name += " ";
376 name += mCommand[i];
377 }
Joe Onorato1754d742016-11-21 17:51:35 -0800378}
379
Yi Jinb592e3b2018-02-01 15:17:04 -0800380CommandSection::~CommandSection() { free(mCommand); }
Joe Onorato1754d742016-11-21 17:51:35 -0800381
Joe Onorato99598ee2019-02-11 15:55:13 +0000382status_t CommandSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700383 FdBuffer buffer;
384 Fpipe cmdPipe;
385 Fpipe ihPipe;
386
387 if (!cmdPipe.init() || !ihPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700388 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700389 return -errno;
390 }
391
Yi Jinc36e91d2018-03-08 11:25:58 -0800392 pid_t cmdPid = fork_execute_cmd((char* const*)mCommand, NULL, &cmdPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700393 if (cmdPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700394 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700395 return -errno;
396 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800397 pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700398 if (ihPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700399 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700400 return -errno;
401 }
402
Yi Jin6355d2f2018-03-14 15:18:02 -0700403 cmdPipe.writeFd().reset();
Yi Jine3dab2d2018-03-22 16:56:39 -0700404 status_t readStatus = buffer.read(ihPipe.readFd().get(), this->timeoutMs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000405 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700406 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700407 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800408 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800409 kill_child(cmdPid);
410 kill_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700411 return readStatus;
412 }
413
Kweku Adamseadd1232018-02-05 16:45:13 -0800414 // Waiting for command here has one trade-off: the failed status of command won't be detected
Yi Jin1a11fa12018-02-22 16:44:10 -0800415 // until buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700416 status_t cmdStatus = wait_child(cmdPid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800417 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700418 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000419 ALOGW("[%s] abnormal child processes, return status: command: %s, incident helper: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800420 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
Joe Onorato99598ee2019-02-11 15:55:13 +0000421 // Not a fatal error.
422 return NO_ERROR;
Yi Jinb44f7d42017-07-21 12:12:59 -0700423 }
424
Joe Onorato99598ee2019-02-11 15:55:13 +0000425 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800426}
427
428// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700429DumpsysSection::DumpsysSection(int id, const char* service, ...)
430 : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), mService(service) {
Joe Onorato1754d742016-11-21 17:51:35 -0800431 name = "dumpsys ";
432 name += service;
433
434 va_list args;
435 va_start(args, service);
436 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700437 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800438 if (arg == NULL) {
439 break;
440 }
441 mArgs.add(String16(arg));
442 name += " ";
443 name += arg;
444 }
445 va_end(args);
446}
447
Yi Jinb592e3b2018-02-01 15:17:04 -0800448DumpsysSection::~DumpsysSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800449
Yi Jinb592e3b2018-02-01 15:17:04 -0800450status_t DumpsysSection::BlockingCall(int pipeWriteFd) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800451 // checkService won't wait for the service to show up like getService will.
452 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700453
Joe Onorato1754d742016-11-21 17:51:35 -0800454 if (service == NULL) {
Joe Onorato1754d742016-11-21 17:51:35 -0800455 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
Mike Ma28381692018-12-04 15:46:29 -0800456 return NAME_NOT_FOUND;
Joe Onorato1754d742016-11-21 17:51:35 -0800457 }
458
459 service->dump(pipeWriteFd, mArgs);
460
461 return NO_ERROR;
462}
Yi Jin3c034c92017-12-22 17:36:47 -0800463
464// ================================================================================
465// initialization only once in Section.cpp.
466map<log_id_t, log_time> LogSection::gLastLogsRetrieved;
467
Yi Jinb592e3b2018-02-01 15:17:04 -0800468LogSection::LogSection(int id, log_id_t logID) : WorkerThreadSection(id), mLogID(logID) {
Yi Jin3be0b432018-04-20 17:08:11 -0700469 name = "logcat ";
Yi Jin3c034c92017-12-22 17:36:47 -0800470 name += android_log_id_to_name(logID);
471 switch (logID) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800472 case LOG_ID_EVENTS:
473 case LOG_ID_STATS:
474 case LOG_ID_SECURITY:
475 mBinary = true;
476 break;
477 default:
478 mBinary = false;
Yi Jin3c034c92017-12-22 17:36:47 -0800479 }
480}
481
Yi Jinb592e3b2018-02-01 15:17:04 -0800482LogSection::~LogSection() {}
Yi Jin3c034c92017-12-22 17:36:47 -0800483
Yi Jinb592e3b2018-02-01 15:17:04 -0800484static size_t trimTail(char const* buf, size_t len) {
Yi Jin3c034c92017-12-22 17:36:47 -0800485 while (len > 0) {
486 char c = buf[len - 1];
487 if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') {
488 len--;
489 } else {
490 break;
491 }
492 }
493 return len;
494}
495
496static inline int32_t get4LE(uint8_t const* src) {
497 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
498}
499
Yi Jinb592e3b2018-02-01 15:17:04 -0800500status_t LogSection::BlockingCall(int pipeWriteFd) const {
Yi Jin3c034c92017-12-22 17:36:47 -0800501 // Open log buffer and getting logs since last retrieved time if any.
502 unique_ptr<logger_list, void (*)(logger_list*)> loggers(
Yi Jinb592e3b2018-02-01 15:17:04 -0800503 gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end()
504 ? android_logger_list_alloc(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0)
505 : android_logger_list_alloc_time(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK,
506 gLastLogsRetrieved[mLogID], 0),
507 android_logger_list_free);
Yi Jin3c034c92017-12-22 17:36:47 -0800508
509 if (android_logger_open(loggers.get(), mLogID) == NULL) {
Yi Jin3be0b432018-04-20 17:08:11 -0700510 ALOGE("[%s] Can't get logger.", this->name.string());
Yi Jin83fb1d52018-03-16 12:03:53 -0700511 return -1;
Yi Jin3c034c92017-12-22 17:36:47 -0800512 }
513
514 log_msg msg;
515 log_time lastTimestamp(0);
516
517 ProtoOutputStream proto;
Yi Jinb592e3b2018-02-01 15:17:04 -0800518 while (true) { // keeps reading until logd buffer is fully read.
Yi Jin83fb1d52018-03-16 12:03:53 -0700519 status_t err = android_logger_list_read(loggers.get(), &msg);
Yi Jin3c034c92017-12-22 17:36:47 -0800520 // err = 0 - no content, unexpected connection drop or EOF.
521 // err = +ive number - size of retrieved data from logger
522 // err = -ive number, OS supplied error _except_ for -EAGAIN
523 // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data.
524 if (err <= 0) {
525 if (err != -EAGAIN) {
Yi Jin3be0b432018-04-20 17:08:11 -0700526 ALOGW("[%s] fails to read a log_msg.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800527 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700528 // dump previous logs and don't consider this error a failure.
Yi Jin3c034c92017-12-22 17:36:47 -0800529 break;
530 }
531 if (mBinary) {
532 // remove the first uint32 which is tag's index in event log tags
533 android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t),
Yi Jinb592e3b2018-02-01 15:17:04 -0800534 msg.len() - sizeof(uint32_t));
535 ;
Yi Jin3c034c92017-12-22 17:36:47 -0800536 android_log_list_element elem;
537
538 lastTimestamp.tv_sec = msg.entry_v1.sec;
539 lastTimestamp.tv_nsec = msg.entry_v1.nsec;
540
541 // format a BinaryLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800542 uint64_t token = proto.start(LogProto::BINARY_LOGS);
Yi Jin3c034c92017-12-22 17:36:47 -0800543 proto.write(BinaryLogEntry::SEC, msg.entry_v1.sec);
544 proto.write(BinaryLogEntry::NANOSEC, msg.entry_v1.nsec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800545 proto.write(BinaryLogEntry::UID, (int)msg.entry_v4.uid);
Yi Jin3c034c92017-12-22 17:36:47 -0800546 proto.write(BinaryLogEntry::PID, msg.entry_v1.pid);
547 proto.write(BinaryLogEntry::TID, msg.entry_v1.tid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800548 proto.write(BinaryLogEntry::TAG_INDEX,
549 get4LE(reinterpret_cast<uint8_t const*>(msg.msg())));
Yi Jin3c034c92017-12-22 17:36:47 -0800550 do {
551 elem = android_log_read_next(context);
Yi Jin5ee07872018-03-05 18:18:27 -0800552 uint64_t elemToken = proto.start(BinaryLogEntry::ELEMS);
Yi Jin3c034c92017-12-22 17:36:47 -0800553 switch (elem.type) {
554 case EVENT_TYPE_INT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800555 proto.write(BinaryLogEntry::Elem::TYPE,
556 BinaryLogEntry::Elem::EVENT_TYPE_INT);
557 proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32);
Yi Jin3c034c92017-12-22 17:36:47 -0800558 break;
559 case EVENT_TYPE_LONG:
Yi Jinb592e3b2018-02-01 15:17:04 -0800560 proto.write(BinaryLogEntry::Elem::TYPE,
561 BinaryLogEntry::Elem::EVENT_TYPE_LONG);
562 proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64);
Yi Jin3c034c92017-12-22 17:36:47 -0800563 break;
564 case EVENT_TYPE_STRING:
Yi Jinb592e3b2018-02-01 15:17:04 -0800565 proto.write(BinaryLogEntry::Elem::TYPE,
566 BinaryLogEntry::Elem::EVENT_TYPE_STRING);
Yi Jin3c034c92017-12-22 17:36:47 -0800567 proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len);
568 break;
569 case EVENT_TYPE_FLOAT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800570 proto.write(BinaryLogEntry::Elem::TYPE,
571 BinaryLogEntry::Elem::EVENT_TYPE_FLOAT);
Yi Jin3c034c92017-12-22 17:36:47 -0800572 proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32);
573 break;
574 case EVENT_TYPE_LIST:
Yi Jinb592e3b2018-02-01 15:17:04 -0800575 proto.write(BinaryLogEntry::Elem::TYPE,
576 BinaryLogEntry::Elem::EVENT_TYPE_LIST);
Yi Jin3c034c92017-12-22 17:36:47 -0800577 break;
578 case EVENT_TYPE_LIST_STOP:
Yi Jinb592e3b2018-02-01 15:17:04 -0800579 proto.write(BinaryLogEntry::Elem::TYPE,
580 BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP);
Yi Jin3c034c92017-12-22 17:36:47 -0800581 break;
582 case EVENT_TYPE_UNKNOWN:
Yi Jinb592e3b2018-02-01 15:17:04 -0800583 proto.write(BinaryLogEntry::Elem::TYPE,
584 BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN);
Yi Jin3c034c92017-12-22 17:36:47 -0800585 break;
586 }
587 proto.end(elemToken);
588 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
589 proto.end(token);
590 if (context) {
591 android_log_destroy(&context);
592 }
593 } else {
594 AndroidLogEntry entry;
595 err = android_log_processLogBuffer(&msg.entry_v1, &entry);
596 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700597 ALOGW("[%s] fails to process to an entry.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800598 break;
599 }
600 lastTimestamp.tv_sec = entry.tv_sec;
601 lastTimestamp.tv_nsec = entry.tv_nsec;
602
603 // format a TextLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800604 uint64_t token = proto.start(LogProto::TEXT_LOGS);
Yi Jin3c034c92017-12-22 17:36:47 -0800605 proto.write(TextLogEntry::SEC, (long long)entry.tv_sec);
606 proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec);
607 proto.write(TextLogEntry::PRIORITY, (int)entry.priority);
608 proto.write(TextLogEntry::UID, entry.uid);
609 proto.write(TextLogEntry::PID, entry.pid);
610 proto.write(TextLogEntry::TID, entry.tid);
611 proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen));
Yi Jinb592e3b2018-02-01 15:17:04 -0800612 proto.write(TextLogEntry::LOG, entry.message,
613 trimTail(entry.message, entry.messageLen));
Yi Jin3c034c92017-12-22 17:36:47 -0800614 proto.end(token);
615 }
616 }
617 gLastLogsRetrieved[mLogID] = lastTimestamp;
Kweku Adams5b763c12018-09-13 15:44:58 -0700618 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
619 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
620 return EPIPE;
621 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700622 return NO_ERROR;
Yi Jin3c034c92017-12-22 17:36:47 -0800623}
Kweku Adamseadd1232018-02-05 16:45:13 -0800624
625// ================================================================================
626
627TombstoneSection::TombstoneSection(int id, const char* type, const int64_t timeoutMs)
628 : WorkerThreadSection(id, timeoutMs), mType(type) {
Yi Jin3be0b432018-04-20 17:08:11 -0700629 name = "tombstone ";
Kweku Adamseadd1232018-02-05 16:45:13 -0800630 name += type;
631}
632
633TombstoneSection::~TombstoneSection() {}
634
635status_t TombstoneSection::BlockingCall(int pipeWriteFd) const {
636 std::unique_ptr<DIR, decltype(&closedir)> proc(opendir("/proc"), closedir);
637 if (proc.get() == nullptr) {
638 ALOGE("opendir /proc failed: %s\n", strerror(errno));
639 return -errno;
640 }
641
642 const std::set<int> hal_pids = get_interesting_hal_pids();
643
644 ProtoOutputStream proto;
645 struct dirent* d;
646 status_t err = NO_ERROR;
647 while ((d = readdir(proc.get()))) {
648 int pid = atoi(d->d_name);
649 if (pid <= 0) {
650 continue;
651 }
652
653 const std::string link_name = android::base::StringPrintf("/proc/%d/exe", pid);
654 std::string exe;
655 if (!android::base::Readlink(link_name, &exe)) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000656 ALOGE("Section %s: Can't read '%s': %s\n", name.string(),
657 link_name.c_str(), strerror(errno));
Kweku Adamseadd1232018-02-05 16:45:13 -0800658 continue;
659 }
660
661 bool is_java_process;
662 if (exe == "/system/bin/app_process32" || exe == "/system/bin/app_process64") {
663 if (mType != "java") continue;
664 // Don't bother dumping backtraces for the zygote.
665 if (IsZygote(pid)) {
666 VLOG("Skipping Zygote");
667 continue;
668 }
669
670 is_java_process = true;
671 } else if (should_dump_native_traces(exe.c_str())) {
672 if (mType != "native") continue;
673 is_java_process = false;
674 } else if (hal_pids.find(pid) != hal_pids.end()) {
675 if (mType != "hal") continue;
676 is_java_process = false;
677 } else {
678 // Probably a native process we don't care about, continue.
679 VLOG("Skipping %d", pid);
680 continue;
681 }
682
683 Fpipe dumpPipe;
684 if (!dumpPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700685 ALOGW("[%s] failed to setup dump pipe", this->name.string());
Kweku Adamseadd1232018-02-05 16:45:13 -0800686 err = -errno;
687 break;
688 }
689
690 const uint64_t start = Nanotime();
691 pid_t child = fork();
692 if (child < 0) {
693 ALOGE("Failed to fork child process");
694 break;
695 } else if (child == 0) {
696 // This is the child process.
Yi Jin6355d2f2018-03-14 15:18:02 -0700697 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800698 const int ret = dump_backtrace_to_file_timeout(
699 pid, is_java_process ? kDebuggerdJavaBacktrace : kDebuggerdNativeBacktrace,
Yi Jin6355d2f2018-03-14 15:18:02 -0700700 is_java_process ? 5 : 20, dumpPipe.writeFd().get());
Kweku Adamseadd1232018-02-05 16:45:13 -0800701 if (ret == -1) {
702 if (errno == 0) {
703 ALOGW("Dumping failed for pid '%d', likely due to a timeout\n", pid);
704 } else {
705 ALOGE("Dumping failed for pid '%d': %s\n", pid, strerror(errno));
706 }
707 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700708 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800709 _exit(EXIT_SUCCESS);
710 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700711 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800712 // Parent process.
713 // Read from the pipe concurrently to avoid blocking the child.
714 FdBuffer buffer;
Yi Jine3dab2d2018-03-22 16:56:39 -0700715 err = buffer.readFully(dumpPipe.readFd().get());
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700716 // Wait on the child to avoid it becoming a zombie process.
717 status_t cStatus = wait_child(child);
Kweku Adamseadd1232018-02-05 16:45:13 -0800718 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700719 ALOGW("[%s] failed to read stack dump: %d", this->name.string(), err);
Yi Jin6355d2f2018-03-14 15:18:02 -0700720 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800721 break;
722 }
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700723 if (cStatus != NO_ERROR) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700724 ALOGE("[%s] child had an issue: %s\n", this->name.string(), strerror(-cStatus));
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700725 }
Kweku Adamseadd1232018-02-05 16:45:13 -0800726
727 auto dump = std::make_unique<char[]>(buffer.size());
Joe Onorato99598ee2019-02-11 15:55:13 +0000728 sp<ProtoReader> reader = buffer.data()->read();
Kweku Adamseadd1232018-02-05 16:45:13 -0800729 int i = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000730 while (reader->hasNext()) {
731 dump[i] = reader->next();
Kweku Adamseadd1232018-02-05 16:45:13 -0800732 i++;
733 }
Yi Jin6cacbcb2018-03-30 14:04:52 -0700734 uint64_t token = proto.start(android::os::BackTraceProto::TRACES);
Kweku Adamseadd1232018-02-05 16:45:13 -0800735 proto.write(android::os::BackTraceProto::Stack::PID, pid);
736 proto.write(android::os::BackTraceProto::Stack::DUMP, dump.get(), i);
737 proto.write(android::os::BackTraceProto::Stack::DUMP_DURATION_NS,
738 static_cast<long long>(Nanotime() - start));
739 proto.end(token);
Yi Jin6355d2f2018-03-14 15:18:02 -0700740 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800741 }
742
Kweku Adams5b763c12018-09-13 15:44:58 -0700743 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
744 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
745 if (err != NO_ERROR) {
746 return EPIPE;
747 }
748 }
749
Kweku Adamseadd1232018-02-05 16:45:13 -0800750 return err;
751}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700752
753} // namespace incidentd
754} // namespace os
Yi Jin98ce8102018-04-12 11:15:39 -0700755} // namespace android