blob: 85c5a20174c3d3c29c8d773176f4625d11871464 [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
Joe Onorato7a406b42019-04-12 18:11:30 -070066bool section_requires_specific_mention(int sectionId) {
67 switch (sectionId) {
68 case 3025: // restricted_images
69 return true;
70 default:
71 return false;
72 }
73}
74
Yi Jin99c248f2017-08-25 18:11:58 -070075// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -070076Section::Section(int i, int64_t timeoutMs)
Kweku Adams3d160912018-05-07 11:26:27 -070077 : id(i),
Joe Onoratofe7bbf42019-03-24 20:57:16 -070078 timeoutMs(timeoutMs) {
79}
Joe Onorato1754d742016-11-21 17:51:35 -080080
Yi Jinb592e3b2018-02-01 15:17:04 -080081Section::~Section() {}
Joe Onorato1754d742016-11-21 17:51:35 -080082
Joe Onorato1754d742016-11-21 17:51:35 -080083// ================================================================================
Yi Jin1a11fa12018-02-22 16:44:10 -080084static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; }
85
Kweku Adamse04ef772018-06-13 12:24:38 -070086FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Joe Onoratofe7bbf42019-03-24 20:57:16 -070087 : Section(id, timeoutMs), mFilename(filename) {
Yi Jin3be0b432018-04-20 17:08:11 -070088 name = "file ";
89 name += filename;
Yi Jin1a11fa12018-02-22 16:44:10 -080090 mIsSysfs = isSysfs(filename);
Yi Jin0a3406f2017-06-22 19:23:11 -070091}
92
93FileSection::~FileSection() {}
94
Joe Onorato99598ee2019-02-11 15:55:13 +000095status_t FileSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -070096 // read from mFilename first, make sure the file is available
97 // add O_CLOEXEC to make sure it is closed when exec incident helper
Yi Jin6355d2f2018-03-14 15:18:02 -070098 unique_fd fd(open(mFilename, O_RDONLY | O_CLOEXEC));
99 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700100 ALOGW("[%s] failed to open file", this->name.string());
Kweku Adamse04ef772018-06-13 12:24:38 -0700101 // There may be some devices/architectures that won't have the file.
102 // Just return here without an error.
103 return NO_ERROR;
Yi Jin0a3406f2017-06-22 19:23:11 -0700104 }
105
Yi Jinb44f7d42017-07-21 12:12:59 -0700106 FdBuffer buffer;
107 Fpipe p2cPipe;
108 Fpipe c2pPipe;
109 // initiate pipes to pass data to/from incident_helper
110 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700111 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700112 return -errno;
113 }
114
Yi Jin1a11fa12018-02-22 16:44:10 -0800115 pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700116 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700117 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700118 return -errno;
119 }
120
121 // parent process
Yi Jine3dab2d2018-03-22 16:56:39 -0700122 status_t readStatus = buffer.readProcessedDataInStream(fd.get(), std::move(p2cPipe.writeFd()),
123 std::move(c2pPipe.readFd()),
124 this->timeoutMs, mIsSysfs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000125 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700126 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700127 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800128 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800129 kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700130 return readStatus;
131 }
132
Yi Jinedfd5bb2017-09-06 17:09:11 -0700133 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700134 if (ihStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700135 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700136 return ihStatus;
137 }
138
Joe Onorato99598ee2019-02-11 15:55:13 +0000139 return writer->writeSection(buffer);
Yi Jin0a3406f2017-06-22 19:23:11 -0700140}
Yi Jin1a11fa12018-02-22 16:44:10 -0800141// ================================================================================
142GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800143 va_list args;
144 va_start(args, filename);
145 mFilenames = varargs(filename, args);
146 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800147 name = "gzip";
148 for (int i = 0; mFilenames[i] != NULL; i++) {
149 name += " ";
150 name += mFilenames[i];
151 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800152}
Yi Jin0a3406f2017-06-22 19:23:11 -0700153
Yi Jin480de782018-04-06 15:37:36 -0700154GZipSection::~GZipSection() { free(mFilenames); }
Yi Jin1a11fa12018-02-22 16:44:10 -0800155
Joe Onorato99598ee2019-02-11 15:55:13 +0000156status_t GZipSection::Execute(ReportWriter* writer) const {
Yi Jin1a11fa12018-02-22 16:44:10 -0800157 // Reads the files in order, use the first available one.
158 int index = 0;
Yi Jin6355d2f2018-03-14 15:18:02 -0700159 unique_fd fd;
Yi Jin1a11fa12018-02-22 16:44:10 -0800160 while (mFilenames[index] != NULL) {
Yi Jin6355d2f2018-03-14 15:18:02 -0700161 fd.reset(open(mFilenames[index], O_RDONLY | O_CLOEXEC));
162 if (fd.get() != -1) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800163 break;
164 }
165 ALOGW("GZipSection failed to open file %s", mFilenames[index]);
166 index++; // look at the next file.
167 }
Yi Jinc858e272018-03-28 15:32:50 -0700168 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700169 ALOGW("[%s] can't open all the files", this->name.string());
Yi Jin6cacbcb2018-03-30 14:04:52 -0700170 return NO_ERROR; // e.g. LAST_KMSG will reach here in user build.
Yi Jinc858e272018-03-28 15:32:50 -0700171 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800172 FdBuffer buffer;
173 Fpipe p2cPipe;
174 Fpipe c2pPipe;
175 // initiate pipes to pass data to/from gzip
176 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700177 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800178 return -errno;
179 }
180
Yi Jinc36e91d2018-03-08 11:25:58 -0800181 pid_t pid = fork_execute_cmd((char* const*)GZIP, &p2cPipe, &c2pPipe);
Yi Jin1a11fa12018-02-22 16:44:10 -0800182 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700183 ALOGW("[%s] failed to fork", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800184 return -errno;
185 }
186 // parent process
187
188 // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using
189 // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream.
Joe Onorato99598ee2019-02-11 15:55:13 +0000190 sp<EncodedBuffer> internalBuffer = buffer.data();
Yi Jin1a11fa12018-02-22 16:44:10 -0800191 internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED);
Yi Jina9635e42018-03-23 12:05:32 -0700192 size_t fileLen = strlen(mFilenames[index]);
193 internalBuffer->writeRawVarint32(fileLen);
194 for (size_t i = 0; i < fileLen; i++) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800195 internalBuffer->writeRawByte(mFilenames[index][i]);
196 }
197 internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA,
198 WIRE_TYPE_LENGTH_DELIMITED);
199 size_t editPos = internalBuffer->wp()->pos();
200 internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size.
201 size_t dataBeginAt = internalBuffer->wp()->pos();
Yi Jin3be0b432018-04-20 17:08:11 -0700202 VLOG("[%s] editPos=%zu, dataBeginAt=%zu", this->name.string(), editPos, dataBeginAt);
Yi Jin1a11fa12018-02-22 16:44:10 -0800203
Yi Jine3dab2d2018-03-22 16:56:39 -0700204 status_t readStatus = buffer.readProcessedDataInStream(
205 fd.get(), std::move(p2cPipe.writeFd()), std::move(c2pPipe.readFd()), this->timeoutMs,
206 isSysfs(mFilenames[index]));
Joe Onorato99598ee2019-02-11 15:55:13 +0000207 writer->setSectionStats(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800208 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700209 ALOGW("[%s] failed to read data from gzip: %s, timedout: %s", this->name.string(),
210 strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin1a11fa12018-02-22 16:44:10 -0800211 kill_child(pid);
212 return readStatus;
213 }
214
215 status_t gzipStatus = wait_child(pid);
216 if (gzipStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700217 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-gzipStatus));
Yi Jin1a11fa12018-02-22 16:44:10 -0800218 return gzipStatus;
219 }
220 // Revisit the actual size from gzip result and edit the internal buffer accordingly.
221 size_t dataSize = buffer.size() - dataBeginAt;
222 internalBuffer->wp()->rewind()->move(editPos);
223 internalBuffer->writeRawVarint32(dataSize);
224 internalBuffer->copy(dataBeginAt, dataSize);
Yi Jin1a11fa12018-02-22 16:44:10 -0800225
Joe Onorato99598ee2019-02-11 15:55:13 +0000226 return writer->writeSection(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800227}
Kweku Adamseadd1232018-02-05 16:45:13 -0800228
Yi Jin0a3406f2017-06-22 19:23:11 -0700229// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800230struct WorkerThreadData : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800231 const WorkerThreadSection* section;
Yi Jin6355d2f2018-03-14 15:18:02 -0700232 Fpipe pipe;
Joe Onorato1754d742016-11-21 17:51:35 -0800233
234 // Lock protects these fields
235 mutex lock;
236 bool workerDone;
237 status_t workerError;
238
Chih-Hung Hsieh7a88a932018-12-20 13:45:04 -0800239 explicit WorkerThreadData(const WorkerThreadSection* section);
Joe Onorato1754d742016-11-21 17:51:35 -0800240 virtual ~WorkerThreadData();
Joe Onorato1754d742016-11-21 17:51:35 -0800241};
242
243WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
Yi Jin6355d2f2018-03-14 15:18:02 -0700244 : section(sec), workerDone(false), workerError(NO_ERROR) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800245
Yi Jinb592e3b2018-02-01 15:17:04 -0800246WorkerThreadData::~WorkerThreadData() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800247
248// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700249WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs)
250 : Section(id, timeoutMs) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800251
Yi Jinb592e3b2018-02-01 15:17:04 -0800252WorkerThreadSection::~WorkerThreadSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800253
Kweku Adams5b763c12018-09-13 15:44:58 -0700254void sigpipe_handler(int signum) {
255 if (signum == SIGPIPE) {
256 ALOGE("Wrote to a broken pipe\n");
257 } else {
258 ALOGE("Received unexpected signal: %d\n", signum);
259 }
260}
261
Yi Jinb592e3b2018-02-01 15:17:04 -0800262static void* worker_thread_func(void* cookie) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700263 // Don't crash the service if we write to a closed pipe (which can happen if
264 // dumping times out).
265 signal(SIGPIPE, sigpipe_handler);
266
Joe Onorato1754d742016-11-21 17:51:35 -0800267 WorkerThreadData* data = (WorkerThreadData*)cookie;
Yi Jin6355d2f2018-03-14 15:18:02 -0700268 status_t err = data->section->BlockingCall(data->pipe.writeFd().get());
Joe Onorato1754d742016-11-21 17:51:35 -0800269
270 {
271 unique_lock<mutex> lock(data->lock);
272 data->workerDone = true;
273 data->workerError = err;
274 }
275
Yi Jin6355d2f2018-03-14 15:18:02 -0700276 data->pipe.writeFd().reset();
Joe Onorato1754d742016-11-21 17:51:35 -0800277 data->decStrong(data->section);
278 // data might be gone now. don't use it after this point in this thread.
279 return NULL;
280}
281
Joe Onorato99598ee2019-02-11 15:55:13 +0000282status_t WorkerThreadSection::Execute(ReportWriter* writer) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800283 status_t err = NO_ERROR;
284 pthread_t thread;
285 pthread_attr_t attr;
Mike Ma28381692018-12-04 15:46:29 -0800286 bool workerDone = false;
Joe Onorato1754d742016-11-21 17:51:35 -0800287 FdBuffer buffer;
288
289 // Data shared between this thread and the worker thread.
290 sp<WorkerThreadData> data = new WorkerThreadData(this);
291
292 // Create the pipe
Yi Jin6355d2f2018-03-14 15:18:02 -0700293 if (!data->pipe.init()) {
Joe Onorato1754d742016-11-21 17:51:35 -0800294 return -errno;
295 }
296
Joe Onorato1754d742016-11-21 17:51:35 -0800297 // Create the thread
298 err = pthread_attr_init(&attr);
299 if (err != 0) {
300 return -err;
301 }
302 // TODO: Do we need to tweak thread priority?
303 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
304 if (err != 0) {
305 pthread_attr_destroy(&attr);
306 return -err;
307 }
Joe Onorato99598ee2019-02-11 15:55:13 +0000308
309 // The worker thread needs a reference and we can't let the count go to zero
310 // if that thread is slow to start.
311 data->incStrong(this);
312
Joe Onorato1754d742016-11-21 17:51:35 -0800313 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
Joe Onorato99598ee2019-02-11 15:55:13 +0000314 pthread_attr_destroy(&attr);
Joe Onorato1754d742016-11-21 17:51:35 -0800315 if (err != 0) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000316 data->decStrong(this);
Joe Onorato1754d742016-11-21 17:51:35 -0800317 return -err;
318 }
Joe Onorato1754d742016-11-21 17:51:35 -0800319
320 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jine3dab2d2018-03-22 16:56:39 -0700321 err = buffer.read(data->pipe.readFd().get(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800322 if (err != NO_ERROR) {
Mike Ma28381692018-12-04 15:46:29 -0800323 ALOGE("[%s] reader failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800324 }
325
326 // Done with the read fd. The worker thread closes the write one so
327 // we never race and get here first.
Yi Jin6355d2f2018-03-14 15:18:02 -0700328 data->pipe.readFd().reset();
Joe Onorato1754d742016-11-21 17:51:35 -0800329
330 // If the worker side is finished, then return its error (which may overwrite
Mike Ma28381692018-12-04 15:46:29 -0800331 // our possible error -- but it's more interesting anyway). If not, then we timed out.
Joe Onorato1754d742016-11-21 17:51:35 -0800332 {
333 unique_lock<mutex> lock(data->lock);
Mike Ma28381692018-12-04 15:46:29 -0800334 if (data->workerError != NO_ERROR) {
335 err = data->workerError;
336 ALOGE("[%s] worker failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800337 }
Mike Ma28381692018-12-04 15:46:29 -0800338 workerDone = data->workerDone;
Joe Onorato1754d742016-11-21 17:51:35 -0800339 }
Kweku Adams5b763c12018-09-13 15:44:58 -0700340
Joe Onorato99598ee2019-02-11 15:55:13 +0000341 writer->setSectionStats(buffer);
Mike Ma28381692018-12-04 15:46:29 -0800342 if (err != NO_ERROR) {
343 char errMsg[128];
344 snprintf(errMsg, 128, "[%s] failed with error '%s'",
345 this->name.string(), strerror(-err));
Joe Onorato99598ee2019-02-11 15:55:13 +0000346 writer->error(this, err, "WorkerThreadSection failed.");
Joe Onorato1754d742016-11-21 17:51:35 -0800347 return NO_ERROR;
348 }
Mike Ma28381692018-12-04 15:46:29 -0800349 if (buffer.truncated()) {
350 ALOGW("[%s] too large, truncating", this->name.string());
Joe Onorato99598ee2019-02-11 15:55:13 +0000351 // Do not write a truncated section. It won't pass through the PrivacyFilter.
Mike Ma28381692018-12-04 15:46:29 -0800352 return NO_ERROR;
353 }
354 if (!workerDone || buffer.timedOut()) {
355 ALOGW("[%s] timed out", this->name.string());
Joe Onorato1754d742016-11-21 17:51:35 -0800356 return NO_ERROR;
357 }
358
359 // Write the data that was collected
Joe Onorato99598ee2019-02-11 15:55:13 +0000360 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800361}
362
363// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700364CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800365 : Section(id, timeoutMs) {
Joe Onorato1754d742016-11-21 17:51:35 -0800366 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700367 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800368 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800369 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800370 name = "cmd";
371 for (int i = 0; mCommand[i] != NULL; i++) {
372 name += " ";
373 name += mCommand[i];
374 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700375}
Joe Onorato1754d742016-11-21 17:51:35 -0800376
Yi Jinb592e3b2018-02-01 15:17:04 -0800377CommandSection::CommandSection(int id, const char* command, ...) : Section(id) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700378 va_list args;
379 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800380 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800381 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800382 name = "cmd";
383 for (int i = 0; mCommand[i] != NULL; i++) {
384 name += " ";
385 name += mCommand[i];
386 }
Joe Onorato1754d742016-11-21 17:51:35 -0800387}
388
Yi Jinb592e3b2018-02-01 15:17:04 -0800389CommandSection::~CommandSection() { free(mCommand); }
Joe Onorato1754d742016-11-21 17:51:35 -0800390
Joe Onorato99598ee2019-02-11 15:55:13 +0000391status_t CommandSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700392 FdBuffer buffer;
393 Fpipe cmdPipe;
394 Fpipe ihPipe;
395
396 if (!cmdPipe.init() || !ihPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700397 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700398 return -errno;
399 }
400
Yi Jinc36e91d2018-03-08 11:25:58 -0800401 pid_t cmdPid = fork_execute_cmd((char* const*)mCommand, NULL, &cmdPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700402 if (cmdPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700403 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700404 return -errno;
405 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800406 pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700407 if (ihPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700408 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700409 return -errno;
410 }
411
Yi Jin6355d2f2018-03-14 15:18:02 -0700412 cmdPipe.writeFd().reset();
Yi Jine3dab2d2018-03-22 16:56:39 -0700413 status_t readStatus = buffer.read(ihPipe.readFd().get(), this->timeoutMs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000414 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700415 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700416 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800417 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800418 kill_child(cmdPid);
419 kill_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700420 return readStatus;
421 }
422
Kweku Adamseadd1232018-02-05 16:45:13 -0800423 // Waiting for command here has one trade-off: the failed status of command won't be detected
Yi Jin1a11fa12018-02-22 16:44:10 -0800424 // until buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700425 status_t cmdStatus = wait_child(cmdPid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800426 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700427 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000428 ALOGW("[%s] abnormal child processes, return status: command: %s, incident helper: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800429 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
Joe Onorato99598ee2019-02-11 15:55:13 +0000430 // Not a fatal error.
431 return NO_ERROR;
Yi Jinb44f7d42017-07-21 12:12:59 -0700432 }
433
Joe Onorato99598ee2019-02-11 15:55:13 +0000434 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800435}
436
437// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700438DumpsysSection::DumpsysSection(int id, const char* service, ...)
439 : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), mService(service) {
Joe Onorato1754d742016-11-21 17:51:35 -0800440 name = "dumpsys ";
441 name += service;
442
443 va_list args;
444 va_start(args, service);
445 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700446 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800447 if (arg == NULL) {
448 break;
449 }
450 mArgs.add(String16(arg));
451 name += " ";
452 name += arg;
453 }
454 va_end(args);
455}
456
Yi Jinb592e3b2018-02-01 15:17:04 -0800457DumpsysSection::~DumpsysSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800458
Yi Jinb592e3b2018-02-01 15:17:04 -0800459status_t DumpsysSection::BlockingCall(int pipeWriteFd) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800460 // checkService won't wait for the service to show up like getService will.
461 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700462
Joe Onorato1754d742016-11-21 17:51:35 -0800463 if (service == NULL) {
Joe Onorato1754d742016-11-21 17:51:35 -0800464 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
Mike Ma28381692018-12-04 15:46:29 -0800465 return NAME_NOT_FOUND;
Joe Onorato1754d742016-11-21 17:51:35 -0800466 }
467
468 service->dump(pipeWriteFd, mArgs);
469
470 return NO_ERROR;
471}
Yi Jin3c034c92017-12-22 17:36:47 -0800472
473// ================================================================================
474// initialization only once in Section.cpp.
475map<log_id_t, log_time> LogSection::gLastLogsRetrieved;
476
Yi Jinb592e3b2018-02-01 15:17:04 -0800477LogSection::LogSection(int id, log_id_t logID) : WorkerThreadSection(id), mLogID(logID) {
Yi Jin3be0b432018-04-20 17:08:11 -0700478 name = "logcat ";
Yi Jin3c034c92017-12-22 17:36:47 -0800479 name += android_log_id_to_name(logID);
480 switch (logID) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800481 case LOG_ID_EVENTS:
482 case LOG_ID_STATS:
483 case LOG_ID_SECURITY:
484 mBinary = true;
485 break;
486 default:
487 mBinary = false;
Yi Jin3c034c92017-12-22 17:36:47 -0800488 }
489}
490
Yi Jinb592e3b2018-02-01 15:17:04 -0800491LogSection::~LogSection() {}
Yi Jin3c034c92017-12-22 17:36:47 -0800492
Yi Jinb592e3b2018-02-01 15:17:04 -0800493static size_t trimTail(char const* buf, size_t len) {
Yi Jin3c034c92017-12-22 17:36:47 -0800494 while (len > 0) {
495 char c = buf[len - 1];
496 if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') {
497 len--;
498 } else {
499 break;
500 }
501 }
502 return len;
503}
504
505static inline int32_t get4LE(uint8_t const* src) {
506 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
507}
508
Yi Jinb592e3b2018-02-01 15:17:04 -0800509status_t LogSection::BlockingCall(int pipeWriteFd) const {
Yi Jin3c034c92017-12-22 17:36:47 -0800510 // Open log buffer and getting logs since last retrieved time if any.
511 unique_ptr<logger_list, void (*)(logger_list*)> loggers(
Yi Jinb592e3b2018-02-01 15:17:04 -0800512 gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end()
513 ? android_logger_list_alloc(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK, 0, 0)
514 : android_logger_list_alloc_time(ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK,
515 gLastLogsRetrieved[mLogID], 0),
516 android_logger_list_free);
Yi Jin3c034c92017-12-22 17:36:47 -0800517
518 if (android_logger_open(loggers.get(), mLogID) == NULL) {
Yi Jin3be0b432018-04-20 17:08:11 -0700519 ALOGE("[%s] Can't get logger.", this->name.string());
Yi Jin83fb1d52018-03-16 12:03:53 -0700520 return -1;
Yi Jin3c034c92017-12-22 17:36:47 -0800521 }
522
523 log_msg msg;
524 log_time lastTimestamp(0);
525
526 ProtoOutputStream proto;
Yi Jinb592e3b2018-02-01 15:17:04 -0800527 while (true) { // keeps reading until logd buffer is fully read.
Yi Jin83fb1d52018-03-16 12:03:53 -0700528 status_t err = android_logger_list_read(loggers.get(), &msg);
Yi Jin3c034c92017-12-22 17:36:47 -0800529 // err = 0 - no content, unexpected connection drop or EOF.
530 // err = +ive number - size of retrieved data from logger
531 // err = -ive number, OS supplied error _except_ for -EAGAIN
532 // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data.
533 if (err <= 0) {
534 if (err != -EAGAIN) {
Yi Jin3be0b432018-04-20 17:08:11 -0700535 ALOGW("[%s] fails to read a log_msg.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800536 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700537 // dump previous logs and don't consider this error a failure.
Yi Jin3c034c92017-12-22 17:36:47 -0800538 break;
539 }
540 if (mBinary) {
541 // remove the first uint32 which is tag's index in event log tags
542 android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t),
Yi Jinb592e3b2018-02-01 15:17:04 -0800543 msg.len() - sizeof(uint32_t));
544 ;
Yi Jin3c034c92017-12-22 17:36:47 -0800545 android_log_list_element elem;
546
547 lastTimestamp.tv_sec = msg.entry_v1.sec;
548 lastTimestamp.tv_nsec = msg.entry_v1.nsec;
549
550 // format a BinaryLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800551 uint64_t token = proto.start(LogProto::BINARY_LOGS);
Yi Jin3c034c92017-12-22 17:36:47 -0800552 proto.write(BinaryLogEntry::SEC, msg.entry_v1.sec);
553 proto.write(BinaryLogEntry::NANOSEC, msg.entry_v1.nsec);
Yi Jinb592e3b2018-02-01 15:17:04 -0800554 proto.write(BinaryLogEntry::UID, (int)msg.entry_v4.uid);
Yi Jin3c034c92017-12-22 17:36:47 -0800555 proto.write(BinaryLogEntry::PID, msg.entry_v1.pid);
556 proto.write(BinaryLogEntry::TID, msg.entry_v1.tid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800557 proto.write(BinaryLogEntry::TAG_INDEX,
558 get4LE(reinterpret_cast<uint8_t const*>(msg.msg())));
Yi Jin3c034c92017-12-22 17:36:47 -0800559 do {
560 elem = android_log_read_next(context);
Yi Jin5ee07872018-03-05 18:18:27 -0800561 uint64_t elemToken = proto.start(BinaryLogEntry::ELEMS);
Yi Jin3c034c92017-12-22 17:36:47 -0800562 switch (elem.type) {
563 case EVENT_TYPE_INT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800564 proto.write(BinaryLogEntry::Elem::TYPE,
565 BinaryLogEntry::Elem::EVENT_TYPE_INT);
566 proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32);
Yi Jin3c034c92017-12-22 17:36:47 -0800567 break;
568 case EVENT_TYPE_LONG:
Yi Jinb592e3b2018-02-01 15:17:04 -0800569 proto.write(BinaryLogEntry::Elem::TYPE,
570 BinaryLogEntry::Elem::EVENT_TYPE_LONG);
571 proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64);
Yi Jin3c034c92017-12-22 17:36:47 -0800572 break;
573 case EVENT_TYPE_STRING:
Yi Jinb592e3b2018-02-01 15:17:04 -0800574 proto.write(BinaryLogEntry::Elem::TYPE,
575 BinaryLogEntry::Elem::EVENT_TYPE_STRING);
Yi Jin3c034c92017-12-22 17:36:47 -0800576 proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len);
577 break;
578 case EVENT_TYPE_FLOAT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800579 proto.write(BinaryLogEntry::Elem::TYPE,
580 BinaryLogEntry::Elem::EVENT_TYPE_FLOAT);
Yi Jin3c034c92017-12-22 17:36:47 -0800581 proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32);
582 break;
583 case EVENT_TYPE_LIST:
Yi Jinb592e3b2018-02-01 15:17:04 -0800584 proto.write(BinaryLogEntry::Elem::TYPE,
585 BinaryLogEntry::Elem::EVENT_TYPE_LIST);
Yi Jin3c034c92017-12-22 17:36:47 -0800586 break;
587 case EVENT_TYPE_LIST_STOP:
Yi Jinb592e3b2018-02-01 15:17:04 -0800588 proto.write(BinaryLogEntry::Elem::TYPE,
589 BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP);
Yi Jin3c034c92017-12-22 17:36:47 -0800590 break;
591 case EVENT_TYPE_UNKNOWN:
Yi Jinb592e3b2018-02-01 15:17:04 -0800592 proto.write(BinaryLogEntry::Elem::TYPE,
593 BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN);
Yi Jin3c034c92017-12-22 17:36:47 -0800594 break;
595 }
596 proto.end(elemToken);
597 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
598 proto.end(token);
599 if (context) {
600 android_log_destroy(&context);
601 }
602 } else {
603 AndroidLogEntry entry;
604 err = android_log_processLogBuffer(&msg.entry_v1, &entry);
605 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700606 ALOGW("[%s] fails to process to an entry.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800607 break;
608 }
609 lastTimestamp.tv_sec = entry.tv_sec;
610 lastTimestamp.tv_nsec = entry.tv_nsec;
611
612 // format a TextLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800613 uint64_t token = proto.start(LogProto::TEXT_LOGS);
Yi Jin3c034c92017-12-22 17:36:47 -0800614 proto.write(TextLogEntry::SEC, (long long)entry.tv_sec);
615 proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec);
616 proto.write(TextLogEntry::PRIORITY, (int)entry.priority);
617 proto.write(TextLogEntry::UID, entry.uid);
618 proto.write(TextLogEntry::PID, entry.pid);
619 proto.write(TextLogEntry::TID, entry.tid);
620 proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen));
Yi Jinb592e3b2018-02-01 15:17:04 -0800621 proto.write(TextLogEntry::LOG, entry.message,
622 trimTail(entry.message, entry.messageLen));
Yi Jin3c034c92017-12-22 17:36:47 -0800623 proto.end(token);
624 }
625 }
626 gLastLogsRetrieved[mLogID] = lastTimestamp;
Kweku Adams5b763c12018-09-13 15:44:58 -0700627 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
628 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
629 return EPIPE;
630 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700631 return NO_ERROR;
Yi Jin3c034c92017-12-22 17:36:47 -0800632}
Kweku Adamseadd1232018-02-05 16:45:13 -0800633
634// ================================================================================
635
636TombstoneSection::TombstoneSection(int id, const char* type, const int64_t timeoutMs)
637 : WorkerThreadSection(id, timeoutMs), mType(type) {
Yi Jin3be0b432018-04-20 17:08:11 -0700638 name = "tombstone ";
Kweku Adamseadd1232018-02-05 16:45:13 -0800639 name += type;
640}
641
642TombstoneSection::~TombstoneSection() {}
643
644status_t TombstoneSection::BlockingCall(int pipeWriteFd) const {
645 std::unique_ptr<DIR, decltype(&closedir)> proc(opendir("/proc"), closedir);
646 if (proc.get() == nullptr) {
647 ALOGE("opendir /proc failed: %s\n", strerror(errno));
648 return -errno;
649 }
650
651 const std::set<int> hal_pids = get_interesting_hal_pids();
652
653 ProtoOutputStream proto;
654 struct dirent* d;
655 status_t err = NO_ERROR;
656 while ((d = readdir(proc.get()))) {
657 int pid = atoi(d->d_name);
658 if (pid <= 0) {
659 continue;
660 }
661
662 const std::string link_name = android::base::StringPrintf("/proc/%d/exe", pid);
663 std::string exe;
664 if (!android::base::Readlink(link_name, &exe)) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000665 ALOGE("Section %s: Can't read '%s': %s\n", name.string(),
666 link_name.c_str(), strerror(errno));
Kweku Adamseadd1232018-02-05 16:45:13 -0800667 continue;
668 }
669
670 bool is_java_process;
671 if (exe == "/system/bin/app_process32" || exe == "/system/bin/app_process64") {
672 if (mType != "java") continue;
673 // Don't bother dumping backtraces for the zygote.
674 if (IsZygote(pid)) {
675 VLOG("Skipping Zygote");
676 continue;
677 }
678
679 is_java_process = true;
680 } else if (should_dump_native_traces(exe.c_str())) {
681 if (mType != "native") continue;
682 is_java_process = false;
683 } else if (hal_pids.find(pid) != hal_pids.end()) {
684 if (mType != "hal") continue;
685 is_java_process = false;
686 } else {
687 // Probably a native process we don't care about, continue.
688 VLOG("Skipping %d", pid);
689 continue;
690 }
691
692 Fpipe dumpPipe;
693 if (!dumpPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700694 ALOGW("[%s] failed to setup dump pipe", this->name.string());
Kweku Adamseadd1232018-02-05 16:45:13 -0800695 err = -errno;
696 break;
697 }
698
699 const uint64_t start = Nanotime();
700 pid_t child = fork();
701 if (child < 0) {
702 ALOGE("Failed to fork child process");
703 break;
704 } else if (child == 0) {
705 // This is the child process.
Yi Jin6355d2f2018-03-14 15:18:02 -0700706 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800707 const int ret = dump_backtrace_to_file_timeout(
708 pid, is_java_process ? kDebuggerdJavaBacktrace : kDebuggerdNativeBacktrace,
Yi Jin6355d2f2018-03-14 15:18:02 -0700709 is_java_process ? 5 : 20, dumpPipe.writeFd().get());
Kweku Adamseadd1232018-02-05 16:45:13 -0800710 if (ret == -1) {
711 if (errno == 0) {
712 ALOGW("Dumping failed for pid '%d', likely due to a timeout\n", pid);
713 } else {
714 ALOGE("Dumping failed for pid '%d': %s\n", pid, strerror(errno));
715 }
716 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700717 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800718 _exit(EXIT_SUCCESS);
719 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700720 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800721 // Parent process.
722 // Read from the pipe concurrently to avoid blocking the child.
723 FdBuffer buffer;
Yi Jine3dab2d2018-03-22 16:56:39 -0700724 err = buffer.readFully(dumpPipe.readFd().get());
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700725 // Wait on the child to avoid it becoming a zombie process.
726 status_t cStatus = wait_child(child);
Kweku Adamseadd1232018-02-05 16:45:13 -0800727 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700728 ALOGW("[%s] failed to read stack dump: %d", this->name.string(), err);
Yi Jin6355d2f2018-03-14 15:18:02 -0700729 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800730 break;
731 }
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700732 if (cStatus != NO_ERROR) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700733 ALOGE("[%s] child had an issue: %s\n", this->name.string(), strerror(-cStatus));
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700734 }
Kweku Adamseadd1232018-02-05 16:45:13 -0800735
736 auto dump = std::make_unique<char[]>(buffer.size());
Joe Onorato99598ee2019-02-11 15:55:13 +0000737 sp<ProtoReader> reader = buffer.data()->read();
Kweku Adamseadd1232018-02-05 16:45:13 -0800738 int i = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000739 while (reader->hasNext()) {
740 dump[i] = reader->next();
Kweku Adamseadd1232018-02-05 16:45:13 -0800741 i++;
742 }
Yi Jin6cacbcb2018-03-30 14:04:52 -0700743 uint64_t token = proto.start(android::os::BackTraceProto::TRACES);
Kweku Adamseadd1232018-02-05 16:45:13 -0800744 proto.write(android::os::BackTraceProto::Stack::PID, pid);
745 proto.write(android::os::BackTraceProto::Stack::DUMP, dump.get(), i);
746 proto.write(android::os::BackTraceProto::Stack::DUMP_DURATION_NS,
747 static_cast<long long>(Nanotime() - start));
748 proto.end(token);
Yi Jin6355d2f2018-03-14 15:18:02 -0700749 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800750 }
751
Kweku Adams5b763c12018-09-13 15:44:58 -0700752 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
753 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
754 if (err != NO_ERROR) {
755 return EPIPE;
756 }
757 }
758
Kweku Adamseadd1232018-02-05 16:45:13 -0800759 return err;
760}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700761
762} // namespace incidentd
763} // namespace os
Yi Jin98ce8102018-04-12 11:15:39 -0700764} // namespace android