blob: c9277a57bd07a0bc648aff88bd6651405f5dc339 [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;
Ryan Savitskicc7d9972019-06-03 23:57:09 +010070 case 3026: // system_trace
71 return true;
Joe Onorato7a406b42019-04-12 18:11:30 -070072 default:
73 return false;
74 }
75}
76
Yi Jin99c248f2017-08-25 18:11:58 -070077// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -070078Section::Section(int i, int64_t timeoutMs)
Kweku Adams3d160912018-05-07 11:26:27 -070079 : id(i),
Joe Onoratofe7bbf42019-03-24 20:57:16 -070080 timeoutMs(timeoutMs) {
81}
Joe Onorato1754d742016-11-21 17:51:35 -080082
Yi Jinb592e3b2018-02-01 15:17:04 -080083Section::~Section() {}
Joe Onorato1754d742016-11-21 17:51:35 -080084
Joe Onorato1754d742016-11-21 17:51:35 -080085// ================================================================================
Yi Jin1a11fa12018-02-22 16:44:10 -080086static inline bool isSysfs(const char* filename) { return strncmp(filename, "/sys/", 5) == 0; }
87
Kweku Adamse04ef772018-06-13 12:24:38 -070088FileSection::FileSection(int id, const char* filename, const int64_t timeoutMs)
Joe Onoratofe7bbf42019-03-24 20:57:16 -070089 : Section(id, timeoutMs), mFilename(filename) {
Yi Jin3be0b432018-04-20 17:08:11 -070090 name = "file ";
91 name += filename;
Yi Jin1a11fa12018-02-22 16:44:10 -080092 mIsSysfs = isSysfs(filename);
Yi Jin0a3406f2017-06-22 19:23:11 -070093}
94
95FileSection::~FileSection() {}
96
Joe Onorato99598ee2019-02-11 15:55:13 +000097status_t FileSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -070098 // read from mFilename first, make sure the file is available
99 // add O_CLOEXEC to make sure it is closed when exec incident helper
Yi Jin6355d2f2018-03-14 15:18:02 -0700100 unique_fd fd(open(mFilename, O_RDONLY | O_CLOEXEC));
101 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700102 ALOGW("[%s] failed to open file", this->name.string());
Kweku Adamse04ef772018-06-13 12:24:38 -0700103 // There may be some devices/architectures that won't have the file.
104 // Just return here without an error.
105 return NO_ERROR;
Yi Jin0a3406f2017-06-22 19:23:11 -0700106 }
107
Yi Jinb44f7d42017-07-21 12:12:59 -0700108 FdBuffer buffer;
109 Fpipe p2cPipe;
110 Fpipe c2pPipe;
111 // initiate pipes to pass data to/from incident_helper
112 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700113 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin0a3406f2017-06-22 19:23:11 -0700114 return -errno;
115 }
116
Yi Jin1a11fa12018-02-22 16:44:10 -0800117 pid_t pid = fork_execute_incident_helper(this->id, &p2cPipe, &c2pPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700118 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700119 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700120 return -errno;
121 }
122
123 // parent process
Yi Jine3dab2d2018-03-22 16:56:39 -0700124 status_t readStatus = buffer.readProcessedDataInStream(fd.get(), std::move(p2cPipe.writeFd()),
125 std::move(c2pPipe.readFd()),
126 this->timeoutMs, mIsSysfs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000127 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700128 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700129 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800130 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800131 kill_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700132 return readStatus;
133 }
134
Yi Jinedfd5bb2017-09-06 17:09:11 -0700135 status_t ihStatus = wait_child(pid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700136 if (ihStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700137 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-ihStatus));
Yi Jinb44f7d42017-07-21 12:12:59 -0700138 return ihStatus;
139 }
140
Joe Onorato99598ee2019-02-11 15:55:13 +0000141 return writer->writeSection(buffer);
Yi Jin0a3406f2017-06-22 19:23:11 -0700142}
Yi Jin1a11fa12018-02-22 16:44:10 -0800143// ================================================================================
144GZipSection::GZipSection(int id, const char* filename, ...) : Section(id) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800145 va_list args;
146 va_start(args, filename);
147 mFilenames = varargs(filename, args);
148 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800149 name = "gzip";
150 for (int i = 0; mFilenames[i] != NULL; i++) {
151 name += " ";
152 name += mFilenames[i];
153 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800154}
Yi Jin0a3406f2017-06-22 19:23:11 -0700155
Yi Jin480de782018-04-06 15:37:36 -0700156GZipSection::~GZipSection() { free(mFilenames); }
Yi Jin1a11fa12018-02-22 16:44:10 -0800157
Joe Onorato99598ee2019-02-11 15:55:13 +0000158status_t GZipSection::Execute(ReportWriter* writer) const {
Yi Jin1a11fa12018-02-22 16:44:10 -0800159 // Reads the files in order, use the first available one.
160 int index = 0;
Yi Jin6355d2f2018-03-14 15:18:02 -0700161 unique_fd fd;
Yi Jin1a11fa12018-02-22 16:44:10 -0800162 while (mFilenames[index] != NULL) {
Yi Jin6355d2f2018-03-14 15:18:02 -0700163 fd.reset(open(mFilenames[index], O_RDONLY | O_CLOEXEC));
164 if (fd.get() != -1) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800165 break;
166 }
167 ALOGW("GZipSection failed to open file %s", mFilenames[index]);
168 index++; // look at the next file.
169 }
Yi Jinc858e272018-03-28 15:32:50 -0700170 if (fd.get() == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700171 ALOGW("[%s] can't open all the files", this->name.string());
Yi Jin6cacbcb2018-03-30 14:04:52 -0700172 return NO_ERROR; // e.g. LAST_KMSG will reach here in user build.
Yi Jinc858e272018-03-28 15:32:50 -0700173 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800174 FdBuffer buffer;
175 Fpipe p2cPipe;
176 Fpipe c2pPipe;
177 // initiate pipes to pass data to/from gzip
178 if (!p2cPipe.init() || !c2pPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700179 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800180 return -errno;
181 }
182
Yi Jinc36e91d2018-03-08 11:25:58 -0800183 pid_t pid = fork_execute_cmd((char* const*)GZIP, &p2cPipe, &c2pPipe);
Yi Jin1a11fa12018-02-22 16:44:10 -0800184 if (pid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700185 ALOGW("[%s] failed to fork", this->name.string());
Yi Jin1a11fa12018-02-22 16:44:10 -0800186 return -errno;
187 }
188 // parent process
189
190 // construct Fdbuffer to output GZippedfileProto, the reason to do this instead of using
191 // ProtoOutputStream is to avoid allocation of another buffer inside ProtoOutputStream.
Joe Onorato99598ee2019-02-11 15:55:13 +0000192 sp<EncodedBuffer> internalBuffer = buffer.data();
Yi Jin1a11fa12018-02-22 16:44:10 -0800193 internalBuffer->writeHeader((uint32_t)GZippedFileProto::FILENAME, WIRE_TYPE_LENGTH_DELIMITED);
Yi Jina9635e42018-03-23 12:05:32 -0700194 size_t fileLen = strlen(mFilenames[index]);
195 internalBuffer->writeRawVarint32(fileLen);
196 for (size_t i = 0; i < fileLen; i++) {
Yi Jin1a11fa12018-02-22 16:44:10 -0800197 internalBuffer->writeRawByte(mFilenames[index][i]);
198 }
199 internalBuffer->writeHeader((uint32_t)GZippedFileProto::GZIPPED_DATA,
200 WIRE_TYPE_LENGTH_DELIMITED);
201 size_t editPos = internalBuffer->wp()->pos();
202 internalBuffer->wp()->move(8); // reserve 8 bytes for the varint of the data size.
203 size_t dataBeginAt = internalBuffer->wp()->pos();
Yi Jin3be0b432018-04-20 17:08:11 -0700204 VLOG("[%s] editPos=%zu, dataBeginAt=%zu", this->name.string(), editPos, dataBeginAt);
Yi Jin1a11fa12018-02-22 16:44:10 -0800205
Yi Jine3dab2d2018-03-22 16:56:39 -0700206 status_t readStatus = buffer.readProcessedDataInStream(
207 fd.get(), std::move(p2cPipe.writeFd()), std::move(c2pPipe.readFd()), this->timeoutMs,
208 isSysfs(mFilenames[index]));
Joe Onorato99598ee2019-02-11 15:55:13 +0000209 writer->setSectionStats(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800210 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700211 ALOGW("[%s] failed to read data from gzip: %s, timedout: %s", this->name.string(),
212 strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin1a11fa12018-02-22 16:44:10 -0800213 kill_child(pid);
214 return readStatus;
215 }
216
217 status_t gzipStatus = wait_child(pid);
218 if (gzipStatus != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700219 ALOGW("[%s] abnormal child process: %s", this->name.string(), strerror(-gzipStatus));
Yi Jin1a11fa12018-02-22 16:44:10 -0800220 return gzipStatus;
221 }
222 // Revisit the actual size from gzip result and edit the internal buffer accordingly.
223 size_t dataSize = buffer.size() - dataBeginAt;
224 internalBuffer->wp()->rewind()->move(editPos);
225 internalBuffer->writeRawVarint32(dataSize);
226 internalBuffer->copy(dataBeginAt, dataSize);
Yi Jin1a11fa12018-02-22 16:44:10 -0800227
Joe Onorato99598ee2019-02-11 15:55:13 +0000228 return writer->writeSection(buffer);
Yi Jin1a11fa12018-02-22 16:44:10 -0800229}
Kweku Adamseadd1232018-02-05 16:45:13 -0800230
Yi Jin0a3406f2017-06-22 19:23:11 -0700231// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800232struct WorkerThreadData : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800233 const WorkerThreadSection* section;
Yi Jin6355d2f2018-03-14 15:18:02 -0700234 Fpipe pipe;
Joe Onorato1754d742016-11-21 17:51:35 -0800235
236 // Lock protects these fields
237 mutex lock;
238 bool workerDone;
239 status_t workerError;
240
Chih-Hung Hsieh7a88a932018-12-20 13:45:04 -0800241 explicit WorkerThreadData(const WorkerThreadSection* section);
Joe Onorato1754d742016-11-21 17:51:35 -0800242 virtual ~WorkerThreadData();
Joe Onorato1754d742016-11-21 17:51:35 -0800243};
244
245WorkerThreadData::WorkerThreadData(const WorkerThreadSection* sec)
Yi Jin6355d2f2018-03-14 15:18:02 -0700246 : section(sec), workerDone(false), workerError(NO_ERROR) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800247
Yi Jinb592e3b2018-02-01 15:17:04 -0800248WorkerThreadData::~WorkerThreadData() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800249
250// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700251WorkerThreadSection::WorkerThreadSection(int id, const int64_t timeoutMs)
252 : Section(id, timeoutMs) {}
Joe Onorato1754d742016-11-21 17:51:35 -0800253
Yi Jinb592e3b2018-02-01 15:17:04 -0800254WorkerThreadSection::~WorkerThreadSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800255
Kweku Adams5b763c12018-09-13 15:44:58 -0700256void sigpipe_handler(int signum) {
257 if (signum == SIGPIPE) {
258 ALOGE("Wrote to a broken pipe\n");
259 } else {
260 ALOGE("Received unexpected signal: %d\n", signum);
261 }
262}
263
Yi Jinb592e3b2018-02-01 15:17:04 -0800264static void* worker_thread_func(void* cookie) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700265 // Don't crash the service if we write to a closed pipe (which can happen if
266 // dumping times out).
267 signal(SIGPIPE, sigpipe_handler);
268
Joe Onorato1754d742016-11-21 17:51:35 -0800269 WorkerThreadData* data = (WorkerThreadData*)cookie;
Yi Jin6355d2f2018-03-14 15:18:02 -0700270 status_t err = data->section->BlockingCall(data->pipe.writeFd().get());
Joe Onorato1754d742016-11-21 17:51:35 -0800271
272 {
273 unique_lock<mutex> lock(data->lock);
274 data->workerDone = true;
275 data->workerError = err;
276 }
277
Yi Jin6355d2f2018-03-14 15:18:02 -0700278 data->pipe.writeFd().reset();
Joe Onorato1754d742016-11-21 17:51:35 -0800279 data->decStrong(data->section);
280 // data might be gone now. don't use it after this point in this thread.
281 return NULL;
282}
283
Joe Onorato99598ee2019-02-11 15:55:13 +0000284status_t WorkerThreadSection::Execute(ReportWriter* writer) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800285 status_t err = NO_ERROR;
286 pthread_t thread;
287 pthread_attr_t attr;
Mike Ma28381692018-12-04 15:46:29 -0800288 bool workerDone = false;
Joe Onorato1754d742016-11-21 17:51:35 -0800289 FdBuffer buffer;
290
291 // Data shared between this thread and the worker thread.
292 sp<WorkerThreadData> data = new WorkerThreadData(this);
293
294 // Create the pipe
Yi Jin6355d2f2018-03-14 15:18:02 -0700295 if (!data->pipe.init()) {
Joe Onorato1754d742016-11-21 17:51:35 -0800296 return -errno;
297 }
298
Joe Onorato1754d742016-11-21 17:51:35 -0800299 // Create the thread
300 err = pthread_attr_init(&attr);
301 if (err != 0) {
302 return -err;
303 }
304 // TODO: Do we need to tweak thread priority?
305 err = pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
306 if (err != 0) {
307 pthread_attr_destroy(&attr);
308 return -err;
309 }
Joe Onorato99598ee2019-02-11 15:55:13 +0000310
311 // The worker thread needs a reference and we can't let the count go to zero
312 // if that thread is slow to start.
313 data->incStrong(this);
314
Joe Onorato1754d742016-11-21 17:51:35 -0800315 err = pthread_create(&thread, &attr, worker_thread_func, (void*)data.get());
Joe Onorato99598ee2019-02-11 15:55:13 +0000316 pthread_attr_destroy(&attr);
Joe Onorato1754d742016-11-21 17:51:35 -0800317 if (err != 0) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000318 data->decStrong(this);
Joe Onorato1754d742016-11-21 17:51:35 -0800319 return -err;
320 }
Joe Onorato1754d742016-11-21 17:51:35 -0800321
322 // Loop reading until either the timeout or the worker side is done (i.e. eof).
Yi Jine3dab2d2018-03-22 16:56:39 -0700323 err = buffer.read(data->pipe.readFd().get(), this->timeoutMs);
Joe Onorato1754d742016-11-21 17:51:35 -0800324 if (err != NO_ERROR) {
Mike Ma28381692018-12-04 15:46:29 -0800325 ALOGE("[%s] reader failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800326 }
327
328 // Done with the read fd. The worker thread closes the write one so
329 // we never race and get here first.
Yi Jin6355d2f2018-03-14 15:18:02 -0700330 data->pipe.readFd().reset();
Joe Onorato1754d742016-11-21 17:51:35 -0800331
332 // If the worker side is finished, then return its error (which may overwrite
Mike Ma28381692018-12-04 15:46:29 -0800333 // our possible error -- but it's more interesting anyway). If not, then we timed out.
Joe Onorato1754d742016-11-21 17:51:35 -0800334 {
335 unique_lock<mutex> lock(data->lock);
Mike Ma28381692018-12-04 15:46:29 -0800336 if (data->workerError != NO_ERROR) {
337 err = data->workerError;
338 ALOGE("[%s] worker failed with error '%s'", this->name.string(), strerror(-err));
Joe Onorato1754d742016-11-21 17:51:35 -0800339 }
Mike Ma28381692018-12-04 15:46:29 -0800340 workerDone = data->workerDone;
Joe Onorato1754d742016-11-21 17:51:35 -0800341 }
Kweku Adams5b763c12018-09-13 15:44:58 -0700342
Joe Onorato99598ee2019-02-11 15:55:13 +0000343 writer->setSectionStats(buffer);
Mike Ma28381692018-12-04 15:46:29 -0800344 if (err != NO_ERROR) {
345 char errMsg[128];
346 snprintf(errMsg, 128, "[%s] failed with error '%s'",
347 this->name.string(), strerror(-err));
Joe Onorato99598ee2019-02-11 15:55:13 +0000348 writer->error(this, err, "WorkerThreadSection failed.");
Joe Onorato1754d742016-11-21 17:51:35 -0800349 return NO_ERROR;
350 }
Mike Ma28381692018-12-04 15:46:29 -0800351 if (buffer.truncated()) {
352 ALOGW("[%s] too large, truncating", this->name.string());
Joe Onorato99598ee2019-02-11 15:55:13 +0000353 // Do not write a truncated section. It won't pass through the PrivacyFilter.
Mike Ma28381692018-12-04 15:46:29 -0800354 return NO_ERROR;
355 }
356 if (!workerDone || buffer.timedOut()) {
357 ALOGW("[%s] timed out", this->name.string());
Joe Onorato1754d742016-11-21 17:51:35 -0800358 return NO_ERROR;
359 }
360
361 // Write the data that was collected
Joe Onorato99598ee2019-02-11 15:55:13 +0000362 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800363}
364
365// ================================================================================
Yi Jinb44f7d42017-07-21 12:12:59 -0700366CommandSection::CommandSection(int id, const int64_t timeoutMs, const char* command, ...)
Yi Jinb592e3b2018-02-01 15:17:04 -0800367 : Section(id, timeoutMs) {
Joe Onorato1754d742016-11-21 17:51:35 -0800368 va_list args;
Yi Jinb44f7d42017-07-21 12:12:59 -0700369 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 }
Yi Jinb44f7d42017-07-21 12:12:59 -0700377}
Joe Onorato1754d742016-11-21 17:51:35 -0800378
Yi Jinb592e3b2018-02-01 15:17:04 -0800379CommandSection::CommandSection(int id, const char* command, ...) : Section(id) {
Yi Jinb44f7d42017-07-21 12:12:59 -0700380 va_list args;
381 va_start(args, command);
Yi Jin1a11fa12018-02-22 16:44:10 -0800382 mCommand = varargs(command, args);
Joe Onorato1754d742016-11-21 17:51:35 -0800383 va_end(args);
Yi Jinc36e91d2018-03-08 11:25:58 -0800384 name = "cmd";
385 for (int i = 0; mCommand[i] != NULL; i++) {
386 name += " ";
387 name += mCommand[i];
388 }
Joe Onorato1754d742016-11-21 17:51:35 -0800389}
390
Yi Jinb592e3b2018-02-01 15:17:04 -0800391CommandSection::~CommandSection() { free(mCommand); }
Joe Onorato1754d742016-11-21 17:51:35 -0800392
Joe Onorato99598ee2019-02-11 15:55:13 +0000393status_t CommandSection::Execute(ReportWriter* writer) const {
Yi Jinb44f7d42017-07-21 12:12:59 -0700394 FdBuffer buffer;
395 Fpipe cmdPipe;
396 Fpipe ihPipe;
397
398 if (!cmdPipe.init() || !ihPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700399 ALOGW("[%s] failed to setup pipes", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700400 return -errno;
401 }
402
Yi Jinc36e91d2018-03-08 11:25:58 -0800403 pid_t cmdPid = fork_execute_cmd((char* const*)mCommand, NULL, &cmdPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700404 if (cmdPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700405 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700406 return -errno;
407 }
Yi Jin1a11fa12018-02-22 16:44:10 -0800408 pid_t ihPid = fork_execute_incident_helper(this->id, &cmdPipe, &ihPipe);
Yi Jinb44f7d42017-07-21 12:12:59 -0700409 if (ihPid == -1) {
Yi Jin3be0b432018-04-20 17:08:11 -0700410 ALOGW("[%s] failed to fork", this->name.string());
Yi Jinb44f7d42017-07-21 12:12:59 -0700411 return -errno;
412 }
413
Yi Jin6355d2f2018-03-14 15:18:02 -0700414 cmdPipe.writeFd().reset();
Yi Jine3dab2d2018-03-22 16:56:39 -0700415 status_t readStatus = buffer.read(ihPipe.readFd().get(), this->timeoutMs);
Joe Onorato99598ee2019-02-11 15:55:13 +0000416 writer->setSectionStats(buffer);
Yi Jinb44f7d42017-07-21 12:12:59 -0700417 if (readStatus != NO_ERROR || buffer.timedOut()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700418 ALOGW("[%s] failed to read data from incident helper: %s, timedout: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800419 this->name.string(), strerror(-readStatus), buffer.timedOut() ? "true" : "false");
Yi Jin4bab3a12018-01-10 16:50:59 -0800420 kill_child(cmdPid);
421 kill_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700422 return readStatus;
423 }
424
Kweku Adamseadd1232018-02-05 16:45:13 -0800425 // Waiting for command here has one trade-off: the failed status of command won't be detected
Yi Jin1a11fa12018-02-22 16:44:10 -0800426 // until buffer timeout, but it has advatage on starting the data stream earlier.
Yi Jinedfd5bb2017-09-06 17:09:11 -0700427 status_t cmdStatus = wait_child(cmdPid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800428 status_t ihStatus = wait_child(ihPid);
Yi Jinb44f7d42017-07-21 12:12:59 -0700429 if (cmdStatus != NO_ERROR || ihStatus != NO_ERROR) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000430 ALOGW("[%s] abnormal child processes, return status: command: %s, incident helper: %s",
Yi Jinb592e3b2018-02-01 15:17:04 -0800431 this->name.string(), strerror(-cmdStatus), strerror(-ihStatus));
Joe Onorato99598ee2019-02-11 15:55:13 +0000432 // Not a fatal error.
433 return NO_ERROR;
Yi Jinb44f7d42017-07-21 12:12:59 -0700434 }
435
Joe Onorato99598ee2019-02-11 15:55:13 +0000436 return writer->writeSection(buffer);
Joe Onorato1754d742016-11-21 17:51:35 -0800437}
438
439// ================================================================================
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700440DumpsysSection::DumpsysSection(int id, const char* service, ...)
441 : WorkerThreadSection(id, REMOTE_CALL_TIMEOUT_MS), mService(service) {
Joe Onorato1754d742016-11-21 17:51:35 -0800442 name = "dumpsys ";
443 name += service;
444
445 va_list args;
446 va_start(args, service);
447 while (true) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700448 const char* arg = va_arg(args, const char*);
Joe Onorato1754d742016-11-21 17:51:35 -0800449 if (arg == NULL) {
450 break;
451 }
452 mArgs.add(String16(arg));
453 name += " ";
454 name += arg;
455 }
456 va_end(args);
457}
458
Yi Jinb592e3b2018-02-01 15:17:04 -0800459DumpsysSection::~DumpsysSection() {}
Joe Onorato1754d742016-11-21 17:51:35 -0800460
Yi Jinb592e3b2018-02-01 15:17:04 -0800461status_t DumpsysSection::BlockingCall(int pipeWriteFd) const {
Joe Onorato1754d742016-11-21 17:51:35 -0800462 // checkService won't wait for the service to show up like getService will.
463 sp<IBinder> service = defaultServiceManager()->checkService(mService);
Yi Jin0a3406f2017-06-22 19:23:11 -0700464
Joe Onorato1754d742016-11-21 17:51:35 -0800465 if (service == NULL) {
Joe Onorato1754d742016-11-21 17:51:35 -0800466 ALOGW("DumpsysSection: Can't lookup service: %s", String8(mService).string());
Mike Ma28381692018-12-04 15:46:29 -0800467 return NAME_NOT_FOUND;
Joe Onorato1754d742016-11-21 17:51:35 -0800468 }
469
470 service->dump(pipeWriteFd, mArgs);
471
472 return NO_ERROR;
473}
Yi Jin3c034c92017-12-22 17:36:47 -0800474
475// ================================================================================
476// initialization only once in Section.cpp.
477map<log_id_t, log_time> LogSection::gLastLogsRetrieved;
478
zhouwenjiec3bf8042019-10-30 14:31:54 -0700479LogSection::LogSection(int id, const char* logID, ...) : WorkerThreadSection(id), mLogMode(logModeBase) {
480 name = "logcat -b ";
481 name += logID;
482
483 va_list args;
484 va_start(args, logID);
485 mLogID = android_name_to_log_id(logID);
486 while(true) {
487 const char* arg = va_arg(args, const char*);
488 if (arg == NULL) {
489 break;
490 }
491 if (!strcmp(arg, "-L")) {
492 // Read from last logcat buffer
493 mLogMode = mLogMode | ANDROID_LOG_PSTORE;
494 }
495 name += " ";
496 name += arg;
497 }
Greg Kaiserfa89cde2019-11-04 06:04:42 -0800498 va_end(args);
zhouwenjiec3bf8042019-10-30 14:31:54 -0700499
500 switch (mLogID) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800501 case LOG_ID_EVENTS:
502 case LOG_ID_STATS:
503 case LOG_ID_SECURITY:
504 mBinary = true;
505 break;
506 default:
507 mBinary = false;
Yi Jin3c034c92017-12-22 17:36:47 -0800508 }
509}
510
Yi Jinb592e3b2018-02-01 15:17:04 -0800511LogSection::~LogSection() {}
Yi Jin3c034c92017-12-22 17:36:47 -0800512
Yi Jinb592e3b2018-02-01 15:17:04 -0800513static size_t trimTail(char const* buf, size_t len) {
Yi Jin3c034c92017-12-22 17:36:47 -0800514 while (len > 0) {
515 char c = buf[len - 1];
516 if (c == '\0' || c == ' ' || c == '\n' || c == '\r' || c == ':') {
517 len--;
518 } else {
519 break;
520 }
521 }
522 return len;
523}
524
525static inline int32_t get4LE(uint8_t const* src) {
526 return src[0] | (src[1] << 8) | (src[2] << 16) | (src[3] << 24);
527}
528
Yi Jinb592e3b2018-02-01 15:17:04 -0800529status_t LogSection::BlockingCall(int pipeWriteFd) const {
Yi Jin3c034c92017-12-22 17:36:47 -0800530 // Open log buffer and getting logs since last retrieved time if any.
531 unique_ptr<logger_list, void (*)(logger_list*)> loggers(
Yi Jinb592e3b2018-02-01 15:17:04 -0800532 gLastLogsRetrieved.find(mLogID) == gLastLogsRetrieved.end()
zhouwenjiec3bf8042019-10-30 14:31:54 -0700533 ? android_logger_list_alloc(mLogMode, 0, 0)
534 : android_logger_list_alloc_time(mLogMode, gLastLogsRetrieved[mLogID], 0),
Yi Jinb592e3b2018-02-01 15:17:04 -0800535 android_logger_list_free);
Yi Jin3c034c92017-12-22 17:36:47 -0800536
537 if (android_logger_open(loggers.get(), mLogID) == NULL) {
Yi Jin3be0b432018-04-20 17:08:11 -0700538 ALOGE("[%s] Can't get logger.", this->name.string());
Yi Jin83fb1d52018-03-16 12:03:53 -0700539 return -1;
Yi Jin3c034c92017-12-22 17:36:47 -0800540 }
541
542 log_msg msg;
543 log_time lastTimestamp(0);
544
545 ProtoOutputStream proto;
Yi Jinb592e3b2018-02-01 15:17:04 -0800546 while (true) { // keeps reading until logd buffer is fully read.
Yi Jin83fb1d52018-03-16 12:03:53 -0700547 status_t err = android_logger_list_read(loggers.get(), &msg);
Yi Jin3c034c92017-12-22 17:36:47 -0800548 // err = 0 - no content, unexpected connection drop or EOF.
549 // err = +ive number - size of retrieved data from logger
550 // err = -ive number, OS supplied error _except_ for -EAGAIN
551 // err = -EAGAIN, graceful indication for ANDRODI_LOG_NONBLOCK that this is the end of data.
552 if (err <= 0) {
553 if (err != -EAGAIN) {
Yi Jin3be0b432018-04-20 17:08:11 -0700554 ALOGW("[%s] fails to read a log_msg.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800555 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700556 // dump previous logs and don't consider this error a failure.
Yi Jin3c034c92017-12-22 17:36:47 -0800557 break;
558 }
559 if (mBinary) {
560 // remove the first uint32 which is tag's index in event log tags
561 android_log_context context = create_android_log_parser(msg.msg() + sizeof(uint32_t),
Yi Jinb592e3b2018-02-01 15:17:04 -0800562 msg.len() - sizeof(uint32_t));
563 ;
Yi Jin3c034c92017-12-22 17:36:47 -0800564 android_log_list_element elem;
565
Tom Cherryd0269892019-10-15 17:10:15 -0700566 lastTimestamp.tv_sec = msg.entry.sec;
567 lastTimestamp.tv_nsec = msg.entry.nsec;
Yi Jin3c034c92017-12-22 17:36:47 -0800568
569 // format a BinaryLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800570 uint64_t token = proto.start(LogProto::BINARY_LOGS);
Tom Cherryd0269892019-10-15 17:10:15 -0700571 proto.write(BinaryLogEntry::SEC, (int32_t)msg.entry.sec);
572 proto.write(BinaryLogEntry::NANOSEC, (int32_t)msg.entry.nsec);
573 proto.write(BinaryLogEntry::UID, (int)msg.entry.uid);
574 proto.write(BinaryLogEntry::PID, msg.entry.pid);
575 proto.write(BinaryLogEntry::TID, (int32_t)msg.entry.tid);
Yi Jinb592e3b2018-02-01 15:17:04 -0800576 proto.write(BinaryLogEntry::TAG_INDEX,
577 get4LE(reinterpret_cast<uint8_t const*>(msg.msg())));
Yi Jin3c034c92017-12-22 17:36:47 -0800578 do {
579 elem = android_log_read_next(context);
Yi Jin5ee07872018-03-05 18:18:27 -0800580 uint64_t elemToken = proto.start(BinaryLogEntry::ELEMS);
Yi Jin3c034c92017-12-22 17:36:47 -0800581 switch (elem.type) {
582 case EVENT_TYPE_INT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800583 proto.write(BinaryLogEntry::Elem::TYPE,
584 BinaryLogEntry::Elem::EVENT_TYPE_INT);
585 proto.write(BinaryLogEntry::Elem::VAL_INT32, (int)elem.data.int32);
Yi Jin3c034c92017-12-22 17:36:47 -0800586 break;
587 case EVENT_TYPE_LONG:
Yi Jinb592e3b2018-02-01 15:17:04 -0800588 proto.write(BinaryLogEntry::Elem::TYPE,
589 BinaryLogEntry::Elem::EVENT_TYPE_LONG);
590 proto.write(BinaryLogEntry::Elem::VAL_INT64, (long long)elem.data.int64);
Yi Jin3c034c92017-12-22 17:36:47 -0800591 break;
592 case EVENT_TYPE_STRING:
Yi Jinb592e3b2018-02-01 15:17:04 -0800593 proto.write(BinaryLogEntry::Elem::TYPE,
594 BinaryLogEntry::Elem::EVENT_TYPE_STRING);
Yi Jin3c034c92017-12-22 17:36:47 -0800595 proto.write(BinaryLogEntry::Elem::VAL_STRING, elem.data.string, elem.len);
596 break;
597 case EVENT_TYPE_FLOAT:
Yi Jinb592e3b2018-02-01 15:17:04 -0800598 proto.write(BinaryLogEntry::Elem::TYPE,
599 BinaryLogEntry::Elem::EVENT_TYPE_FLOAT);
Yi Jin3c034c92017-12-22 17:36:47 -0800600 proto.write(BinaryLogEntry::Elem::VAL_FLOAT, elem.data.float32);
601 break;
602 case EVENT_TYPE_LIST:
Yi Jinb592e3b2018-02-01 15:17:04 -0800603 proto.write(BinaryLogEntry::Elem::TYPE,
604 BinaryLogEntry::Elem::EVENT_TYPE_LIST);
Yi Jin3c034c92017-12-22 17:36:47 -0800605 break;
606 case EVENT_TYPE_LIST_STOP:
Yi Jinb592e3b2018-02-01 15:17:04 -0800607 proto.write(BinaryLogEntry::Elem::TYPE,
608 BinaryLogEntry::Elem::EVENT_TYPE_LIST_STOP);
Yi Jin3c034c92017-12-22 17:36:47 -0800609 break;
610 case EVENT_TYPE_UNKNOWN:
Yi Jinb592e3b2018-02-01 15:17:04 -0800611 proto.write(BinaryLogEntry::Elem::TYPE,
612 BinaryLogEntry::Elem::EVENT_TYPE_UNKNOWN);
Yi Jin3c034c92017-12-22 17:36:47 -0800613 break;
614 }
615 proto.end(elemToken);
616 } while ((elem.type != EVENT_TYPE_UNKNOWN) && !elem.complete);
617 proto.end(token);
618 if (context) {
619 android_log_destroy(&context);
620 }
621 } else {
622 AndroidLogEntry entry;
Tom Cherryd0269892019-10-15 17:10:15 -0700623 err = android_log_processLogBuffer(&msg.entry, &entry);
Yi Jin3c034c92017-12-22 17:36:47 -0800624 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700625 ALOGW("[%s] fails to process to an entry.\n", this->name.string());
Yi Jin3c034c92017-12-22 17:36:47 -0800626 break;
627 }
628 lastTimestamp.tv_sec = entry.tv_sec;
629 lastTimestamp.tv_nsec = entry.tv_nsec;
630
631 // format a TextLogEntry
Yi Jin5ee07872018-03-05 18:18:27 -0800632 uint64_t token = proto.start(LogProto::TEXT_LOGS);
Yi Jin3c034c92017-12-22 17:36:47 -0800633 proto.write(TextLogEntry::SEC, (long long)entry.tv_sec);
634 proto.write(TextLogEntry::NANOSEC, (long long)entry.tv_nsec);
635 proto.write(TextLogEntry::PRIORITY, (int)entry.priority);
636 proto.write(TextLogEntry::UID, entry.uid);
637 proto.write(TextLogEntry::PID, entry.pid);
638 proto.write(TextLogEntry::TID, entry.tid);
639 proto.write(TextLogEntry::TAG, entry.tag, trimTail(entry.tag, entry.tagLen));
Yi Jinb592e3b2018-02-01 15:17:04 -0800640 proto.write(TextLogEntry::LOG, entry.message,
641 trimTail(entry.message, entry.messageLen));
Yi Jin3c034c92017-12-22 17:36:47 -0800642 proto.end(token);
643 }
644 }
645 gLastLogsRetrieved[mLogID] = lastTimestamp;
Kweku Adams5b763c12018-09-13 15:44:58 -0700646 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
647 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
648 return EPIPE;
649 }
Yi Jin83fb1d52018-03-16 12:03:53 -0700650 return NO_ERROR;
Yi Jin3c034c92017-12-22 17:36:47 -0800651}
Kweku Adamseadd1232018-02-05 16:45:13 -0800652
653// ================================================================================
654
655TombstoneSection::TombstoneSection(int id, const char* type, const int64_t timeoutMs)
656 : WorkerThreadSection(id, timeoutMs), mType(type) {
Yi Jin3be0b432018-04-20 17:08:11 -0700657 name = "tombstone ";
Kweku Adamseadd1232018-02-05 16:45:13 -0800658 name += type;
659}
660
661TombstoneSection::~TombstoneSection() {}
662
663status_t TombstoneSection::BlockingCall(int pipeWriteFd) const {
664 std::unique_ptr<DIR, decltype(&closedir)> proc(opendir("/proc"), closedir);
665 if (proc.get() == nullptr) {
666 ALOGE("opendir /proc failed: %s\n", strerror(errno));
667 return -errno;
668 }
669
670 const std::set<int> hal_pids = get_interesting_hal_pids();
671
672 ProtoOutputStream proto;
673 struct dirent* d;
674 status_t err = NO_ERROR;
675 while ((d = readdir(proc.get()))) {
676 int pid = atoi(d->d_name);
677 if (pid <= 0) {
678 continue;
679 }
680
681 const std::string link_name = android::base::StringPrintf("/proc/%d/exe", pid);
682 std::string exe;
683 if (!android::base::Readlink(link_name, &exe)) {
Joe Onorato99598ee2019-02-11 15:55:13 +0000684 ALOGE("Section %s: Can't read '%s': %s\n", name.string(),
685 link_name.c_str(), strerror(errno));
Kweku Adamseadd1232018-02-05 16:45:13 -0800686 continue;
687 }
688
689 bool is_java_process;
690 if (exe == "/system/bin/app_process32" || exe == "/system/bin/app_process64") {
691 if (mType != "java") continue;
692 // Don't bother dumping backtraces for the zygote.
693 if (IsZygote(pid)) {
694 VLOG("Skipping Zygote");
695 continue;
696 }
697
698 is_java_process = true;
699 } else if (should_dump_native_traces(exe.c_str())) {
700 if (mType != "native") continue;
701 is_java_process = false;
702 } else if (hal_pids.find(pid) != hal_pids.end()) {
703 if (mType != "hal") continue;
704 is_java_process = false;
705 } else {
706 // Probably a native process we don't care about, continue.
707 VLOG("Skipping %d", pid);
708 continue;
709 }
710
711 Fpipe dumpPipe;
712 if (!dumpPipe.init()) {
Yi Jin3be0b432018-04-20 17:08:11 -0700713 ALOGW("[%s] failed to setup dump pipe", this->name.string());
Kweku Adamseadd1232018-02-05 16:45:13 -0800714 err = -errno;
715 break;
716 }
717
718 const uint64_t start = Nanotime();
719 pid_t child = fork();
720 if (child < 0) {
721 ALOGE("Failed to fork child process");
722 break;
723 } else if (child == 0) {
724 // This is the child process.
Yi Jin6355d2f2018-03-14 15:18:02 -0700725 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800726 const int ret = dump_backtrace_to_file_timeout(
727 pid, is_java_process ? kDebuggerdJavaBacktrace : kDebuggerdNativeBacktrace,
Yi Jin6355d2f2018-03-14 15:18:02 -0700728 is_java_process ? 5 : 20, dumpPipe.writeFd().get());
Kweku Adamseadd1232018-02-05 16:45:13 -0800729 if (ret == -1) {
730 if (errno == 0) {
731 ALOGW("Dumping failed for pid '%d', likely due to a timeout\n", pid);
732 } else {
733 ALOGE("Dumping failed for pid '%d': %s\n", pid, strerror(errno));
734 }
735 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700736 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800737 _exit(EXIT_SUCCESS);
738 }
Yi Jin6355d2f2018-03-14 15:18:02 -0700739 dumpPipe.writeFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800740 // Parent process.
741 // Read from the pipe concurrently to avoid blocking the child.
742 FdBuffer buffer;
Yi Jine3dab2d2018-03-22 16:56:39 -0700743 err = buffer.readFully(dumpPipe.readFd().get());
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700744 // Wait on the child to avoid it becoming a zombie process.
745 status_t cStatus = wait_child(child);
Kweku Adamseadd1232018-02-05 16:45:13 -0800746 if (err != NO_ERROR) {
Yi Jin3be0b432018-04-20 17:08:11 -0700747 ALOGW("[%s] failed to read stack dump: %d", this->name.string(), err);
Yi Jin6355d2f2018-03-14 15:18:02 -0700748 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800749 break;
750 }
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700751 if (cStatus != NO_ERROR) {
Kweku Adams5b763c12018-09-13 15:44:58 -0700752 ALOGE("[%s] child had an issue: %s\n", this->name.string(), strerror(-cStatus));
Kweku Adamsa8a56c82018-04-23 06:15:31 -0700753 }
Kweku Adamseadd1232018-02-05 16:45:13 -0800754
755 auto dump = std::make_unique<char[]>(buffer.size());
Joe Onorato99598ee2019-02-11 15:55:13 +0000756 sp<ProtoReader> reader = buffer.data()->read();
Kweku Adamseadd1232018-02-05 16:45:13 -0800757 int i = 0;
Joe Onorato99598ee2019-02-11 15:55:13 +0000758 while (reader->hasNext()) {
759 dump[i] = reader->next();
Kweku Adamseadd1232018-02-05 16:45:13 -0800760 i++;
761 }
Yi Jin6cacbcb2018-03-30 14:04:52 -0700762 uint64_t token = proto.start(android::os::BackTraceProto::TRACES);
Kweku Adamseadd1232018-02-05 16:45:13 -0800763 proto.write(android::os::BackTraceProto::Stack::PID, pid);
764 proto.write(android::os::BackTraceProto::Stack::DUMP, dump.get(), i);
765 proto.write(android::os::BackTraceProto::Stack::DUMP_DURATION_NS,
766 static_cast<long long>(Nanotime() - start));
767 proto.end(token);
Yi Jin6355d2f2018-03-14 15:18:02 -0700768 dumpPipe.readFd().reset();
Kweku Adamseadd1232018-02-05 16:45:13 -0800769 }
770
Kweku Adams5b763c12018-09-13 15:44:58 -0700771 if (!proto.flush(pipeWriteFd) && errno == EPIPE) {
772 ALOGE("[%s] wrote to a broken pipe\n", this->name.string());
773 if (err != NO_ERROR) {
774 return EPIPE;
775 }
776 }
777
Kweku Adamseadd1232018-02-05 16:45:13 -0800778 return err;
779}
Yi Jin6cacbcb2018-03-30 14:04:52 -0700780
781} // namespace incidentd
782} // namespace os
Yi Jin98ce8102018-04-12 11:15:39 -0700783} // namespace android